Customizing Site-Wide Behavior for ASP.NET Web Pages (Razor) Sites
By Tom FitzMacken|February 17, 2014
 

This article explains how to make site-side settings for pages in an ASP.NET Web Pages (Razor) website.

What you'll learn:

  • How to run code that lets you set values (global values or helper settings) for all pages in a site.
  • How to run code that lets you set values for all pages in a folder.
  • How to run code before and after a page loads.
  • How to send errors to a central error page.
  • How to add authentication to all pages in a folder.

Software versions used in the tutorial

Adding Website Startup Code for ASP.NET Web Pages

For much of the code that you write in ASP.NET Web Pages, an individual page can contain all the code that's required for that page. For example, if a page sends an email message, it's possible to put all the code for that operation in a single page. This can include the code to initialize the settings for sending email (that is, for the SMTP server) and for sending the email message.

However, in some situations, you might want to run some code before any page on the site runs. This is useful for setting values that can be used anywhere in the site (referred to as global values.)  For example, some helpers require you to provide values like email settings or account keys. It can be handy to keep these settings in global values.

You can do this by creating a page named _AppStart.cshtml in the root of the site. If this page exists, it runs the first time any page in the site is requested. Therefore, it's a good place to run code to set global values. (Because_AppStart.cshtml has an underscore prefix, ASP.NET won't send the page to a browser even if users request it directly.)

The following diagram shows how the _AppStart.cshtml page works. When a request comes in for a page, and if this is the first request for any page in the site, ASP.NET first checks whether a _AppStart.cshtml page exists. If so, any code in the _AppStart.cshtml page runs, and then the requested page runs.

Setting Global Values for Your Website

  1. In the root folder of a WebMatrix website, create a file named _AppStart.cshtml. The file must be in the root of the site.
  2. Replace the existing content with the following:
    @{
      AppState["customAppName"] = "Application Name";
    }

    This code stores a value in the AppState dictionary, which is automatically available to all pages in the site. Notice that the _AppStart.cshtml file does not have any markup in it. The page will run the code and then redirect to the page that was originally requested.

    Note       Be careful when you put code in the _AppStart.cshtml file.     If any errors occur in code in the _AppStart.cshtml file, the website won't start.

  3. In the root folder, create a new page named AppName.cshtml.
  4. Replace the default markup and code with the following:
    <!DOCTYPE html>
    <html>
        <head>
            <title>Show Application Name</title>
        </head>
        <body>
            <h1>@AppState["customAppName"]</h1>
        </body>
    </html>

    This code extracts the value from the AppState object that you set in the _AppStart.cshtml page.

  5. Run the AppName.cshtml page in a browser. (Make sure the page is selected in the Files workspace before you run it.) The page displays the global value.

Setting Values for Helpers

A good use for the _AppStart.cshtml file is to set values for helpers that you use in your site and that have to be initialized. Typical examples are email settings for the WebMail helper and the private and public keys for theReCaptcha helper. In cases like these, you can set the values once in the _AppStart.cshtml and then they're already set for all the pages in your site.

This procedure shows you how to set WebMail settings globally. (For more information about using the WebMailhelper, see Adding Email to an ASP.NET Web Pages Site.)

  1. Add the ASP.NET Web Helpers Library to your website as described in Installing Helpers in an ASP.NET Web Pages Site, if you haven't already added it.
  2. If you don't already have a _AppStart.cshtml file, in the root folder of a website create a file named_AppStart.cshtml.
  3. Add the following WebMail settings to the _AppStart.cshtml file:
    @{
        // Initialize WebMail helper
         WebMail.SmtpServer = "your-SMTP-host";
         WebMail.SmtpPort = 25;
         WebMail.UserName = "your-user-name-here";
         WebMail.Password = "your-account-password";
         WebMail.From = "your-email-address-here";
    }

    Modify the following email related settings in the code:

    • Set your-SMTP-host to the name of the SMTP server that you have access to.
    • Set your-user-name-here to the user name for your SMTP server account.
    • Set your-account-password to the password for your SMTP server account.
    • Set your-email-address-here to your own email address. This is the email address that the message is sent from. (Some email providers don't let you specify a different From address and will use your user name as the From address.)

    For more information about SMTP settings, see Configuring Email Settings in the article Sending Email from an ASP.NET Web Pages (Razor) Site and Issues with Sending Email in the ASP.NET Web Pages (Razor) Troubleshooting Guide.

  4. Save the _AppStart.cshtml file and close it.
  5. In the root folder of a website, create new page named TestEmail.cshtml.
  6. Replace the existing content with the following:
    @{
        var message = "";
        try{
            if(IsPost){
                WebMail.Send(
                    to: Request.Form["emailAddress"],
                    subject: Request.Form["emailSubject"],
                    body:Request.Form["emailBody"]
               );
               message = "Email sent!";
            }
        }
        catch(Exception ex){
            message = "Email could not be sent!";
        }
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
         <meta charset="utf-8" />
         <title>Test Email</title>
      </head>
      <body>
        <h1>Test Email</h1>
        <form method="post">
          <p>
            <label for="emailAddress">Email address:</label>
            <input type="text" name="emailAddress" />
          </p>
          <p>
            <label for="emailSubject">Subject:</label>
            <input type="text" name="emailSubject" />
          </p>
          <p>
            <label for="emailBody">Text to send:</label><br/>
            <textarea name="emailBody" rows="6"></textarea>
          </p>
        <p><input type="submit" value="Send!" /></p>
        @if(IsPost){
            <p>@message</p>
        }
        </form>
      </body>
    </html>
  7. Run the TestEmail.cshtml page in a browser.
  8. Fill in the fields to send yourself an email message and then click Send.
  9. Check your email to make sure you've gotten the message.

The important part of this example is that the settings that you don't usually change — like the name of your SMTP server and your email credentials — are set in the _AppStart.cshtml file. That way you don't need to set them again in each page where you send email. (Although if for some reason you need to change those settings, you can set them individually in a page.) In the page, you only set the values that typically change each time, like the recipient and the body of the email message.

Running Code Before and After Files in a Folder

Just like you can use _AppStart.cshtml to write code before pages in the site run, you can write code that runs before (and after) any page in a particular folder run. This is useful for things like setting the same layout page for all the pages in a folder, or for checking that a user is logged in before running a page in the folder.

For pages in particular folders, you can create code in a file named _PageStart.cshtml. The following diagram shows how the _PageStart.cshtml page works. When a request comes in for a page, ASP.NET first checks for a_AppStart.cshtml page and runs that. Then ASP.NET checks whether there's a _PageStart.cshtml page, and if so, runs that. It then runs the requested page.

Inside the _PageStart.cshtml page, you can specify where during processing you want the requested page to run by including a RunPage method. This lets you run code before the requested page runs and then again after it. If you don't include RunPage, all the code in _PageStart.cshtml runs, and then the requested page runs automatically.

ASP.NET lets you create a hierarchy of _PageStart.cshtml files. You can put a _PageStart.cshtml file in the root of the site and in any subfolder. When a page is requested, the _PageStart.cshtml file at the top-most level (nearest to the site root) runs, followed by the _PageStart.cshtml file in the next subfolder, and so on down the subfolder structure until the request reaches the folder that contains the requested page. After all the applicable _PageStart.cshtml files have run, the requested page runs.

For example, you might have the following combination of _PageStart.cshtml files and Default.cshtml file:

@* ~/_PageStart.cshtml *@
@{
  PageData["Color1"] = "Red";
  PageData["Color2"] = "Blue";
}
@* ~/myfolder/_PageStart.cshtml *@
@{
  PageData["Color2"] = "Yellow";
  PageData["Color3"] = "Green";
}
@* ~/myfolder/default.cshtml *@
@PageData["Color1"]
<br/>
@PageData["Color2"]
<br/>
@PageData["Color3"]

When you run /myfolder/default.cshtml, you'll see the following:

Red

Yellow

Green

Running Initialization Code for All Pages in a Folder

A good use for _PageStart.cshtml files is to initialize the same layout page for all files in a single folder.

  1. In the root folder, create a new folder named InitPages.
  2. In the InitPages folder of your website, create a file named _PageStart.cshtml and replace the default markup and code with the following:
    @{
        // Sets the layout page for all pages in the folder.
        Layout = "~/Shared/_Layout1.cshtml";     // Sets a variable available to all pages in the folder.
        PageData["MyBackground"] = "Yellow";
    }
  3. In the root of the website, create a folder named Shared.
  4. In the Shared folder, create a file named _Layout1.cshtml and replace the default markup and code with the following:
    @{
      var backgroundColor = PageData["MyBackground"];
    }
    <!DOCTYPE html>
    <html>
    <head>
      <title>Page Title</title>
      <link type="text/css" href="/Styles/Site.css" rel="stylesheet" />
    </head>
    <body>
      <div id="header">
        Using the _PageStart.cshtml file
      </div>
      <div id="main" style="background-color:@backgroundColor">
        @RenderBody()
      </div>
    <div id="footer">
      &copy; 2012 Contoso. All rights reserved
    </div>
    </body>
    </html>
  5. In the InitPages folder, create a file named Content1.cshtml and replace the existing content with the following:
    <p>This is content page 1.</p>
  6. In the InitPages folder, create another file named Content2.cshtml and replace the default markup with the following:
    <p>This is content page 2.</p>
  7. Run Content1.cshtml in a browser.

    When the Content1.cshtml page runs, the _PageStart.cshtml file sets Layout and also setsPageData["MyBackground"] to a color. In Content1.cshtml, the layout and color are applied.

  8. Display Content2.cshtml in a browser.

    The layout is the same, because both pages use the same layout page and color as initialized in_PageStart.cshtml.

Using _PageStart.cshtml to Handle Errors

Another good use for the _PageStart.cshtml file is to create a way to handle programming errors (exceptions) that might occur in any .cshtml page in a folder. This example shows you one way to do this.

  1. In the root folder, create a folder named InitCatch.
  2. In the InitCatch folder of your website, create a file named _PageStart.cshtml and replace the existing markup and code with the following:
    @{
        try
        {
            RunPage();
        }
        catch (Exception ex)
        {
            Response.Redirect("~/Error.cshtml?source=" +
                HttpUtility.UrlEncode(Request.AppRelativeCurrentExecutionFilePath));
        }
    }

    In this code, you try running the requested page explicitly by calling the RunPage method inside a try block. If any programming errors occur in the requested page, the code inside the catch block runs. In this case, the code redirects to a page (Error.cshtml) and passes the name of the file that experienced the error as part of the URL. (You'll create the page shortly.)

  3. In the InitCatch folder of your website, create a file named Exception.cshtml and replace the existing markup and code with the following:
    @{
        var db = Database.Open("invalidDatabaseFile");
    }

    For purposes of this example, what you're doing in this page is deliberately creating an error by trying to open a database file that doesn't exist.

  4. In the root folder, create a file named Error.cshtml and replace the existing markup and code with the following:
    <!DOCTYPE html>
    <html>
        <head>
            <title>Error Page</title>
        </head>
        <body>
    <h1>Error report</h1>
    <p>An error occurred while running the following file: @Request["source"]</p>
        </body>
    </html>

    In this page, the expression @Request["source"] gets the value out of the URL and displays it.

  5. In the toolbar, click Save.
  6. Run Exception.cshtml in a browser.

    Because an error occurs in Exception.cshtml, the _PageStart.cshtml page redirects to the Error.cshtml file, which displays the message.

    For more information about exceptions, see   Introduction to ASP.NET Web Pages Programming Using the Razor Syntax.

Using _PageStart.cshtml to Restrict Folder Access

You can also use the _PageStart.cshtml file to restrict access to all the files in a folder.

  1. In WebMatrix, create a new website using the Site From Template option.
  2. From the available templates, select Starter Site.
  3. In the root folder, create a folder named AuthenticatedContent.
  4. In the AuthenticatedContent folder, create a file named _PageStart.cshtml and replace the existing markup and code with the following:
    @{
        Response.CacheControl = "no-cache";
        if (!WebSecurity.IsAuthenticated) {
            var returnUrl = "~/Account/Login?ReturnUrl=" + Request.Url.LocalPath;
            Response.Redirect(returnUrl);
        }
    }

    The code starts by preventing all files in the folder from being cached. (This is required for scenarios like public computers, where you don't want one user's cached pages to be available to the next user.) Next, the code determines whether the user has signed in to the site before they can view any of the pages in the folder. If the user is not signed in, the code redirects to the login page. The login page can return the user to the page that was originally requested if you include a query string value named ReturnUrl.

  5. Create a new page in the AuthenticatedContent folder named Page.cshtml.
  6. Replace the default markup with the following:
    @{
        Layout = "~/_SiteLayout.cshtml";
        Page.Title = "Authenticated Content";
    }
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
      </head>
      <body>
        Thank you for authenticating!
      </body>
    </html>
  7. Run Page.cshtml in a browser. The code redirects you to a login page. You must register before logging in. After you've registered and logged in, you can navigate to the page and view its contents.

_AppStart.cshtml 和 _PageStart.cshtml的妙用的更多相关文章

  1. _appstart.cshtml,_pagestart.cshtml,_viewstart.cshtml

    ASP.NET MVC3 系列教程 – Web Pages 1.0 I:Web Pages 1.0中以"_"开头的特别文件(文件命名时不区分大小写) "_appstart ...

  2. ASP.NET MVC3 系列教程 – Web Pages 1.0

    http://www.cnblogs.com/highend/archive/2011/04/14/aspnet_mvc3_web_pages.html I:Web Pages 1.0中以“_”开头的 ...

  3. Customizing Site-Wide Behavior for ASP.NET Web Pages (Razor) Sites

    Customizing Site-Wide Behavior for ASP.NET Web Pages (Razor) Sites By Tom FitzMacken|February 17, 20 ...

  4. 了解.net mvc实现原理ActionResult/View

    了解.net mvc实现原理ActionResult/View 上一篇了解了请求至Controller的Action过程,这篇继续看源码处理Action收到请求数据再返回ActionResult到Vi ...

  5. PartialViewResult不鸟_ViewStart.cshtml

    概述 在ASP.NET MVC中,对于Action中得到的ActionResult如果是一个ViewResult对象,那么在进行View呈现时,则会先执行_ViewStart.cshtml,然后再去执 ...

  6. _viewstart.cshtml的作用

    在ASP.NET MVC 3.0及更高版本中,用Razor模板引擎新建项目后,Views目录下会出现一个这样的文件:_ViewStart.cshtml. _viewstart.cshtml的作用 1. ...

  7. MVC| Razor 布局-模板页 | ViewStart.cshtml

    来自:http://blog.csdn.net/fanbin168/article/details/49725175 这个图就看明白了 _ViewStart.cshtml 视图文件的作用  _View ...

  8. Asp.Net Core 入门(五)—— 布局视图_Layout.cshtml

    布局视图和我们在Asp.Net MVC一样,布局视图_Layout.cshtml使得所有视图保持一致的外观变得更加容易,因为我们只有一个要修改的布局视图文件,更改后将立即反映在整个应用程序的所有视图中 ...

  9. ASP.NET MVC教程八:_ViewStart.cshtml

    一.引言 _ViewStart.cshtml是在ASP.NET MVC 3.0及更高版本以后出现的,用Razor模板引擎新建项目后,Views目录下面会出现一个这样的文件: 打开_ViewStart. ...

随机推荐

  1. python-mao

    冒泡排序算法的运作如下: 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数. 针对所有的元素重复 ...

  2. Velodyne 线性激光雷达数据合成

    坐标系旋转 如果想用字母表示角度,有两个方法: 1.  用三角函数sind(θ4).cosd(θ4).tand(θ4).atand(θ4)进行表示,注意:θ4在输入时是角度,只是没有度数特有的符号(° ...

  3. Java-Maven-Runoob:Maven 构建 & 项目测试

    ylbtech-Java-Maven-Runoob:Maven 构建 & 项目测试 1.返回顶部 1. Maven 构建 & 项目测试 在上一章节中我们学会了如何使用 Maven 创建 ...

  4. 模块的分类以及time与date time 模块 radom模块

    1.标准库,或者内置模块,python解释器自带的,比如sys,os模块 2.开源模块,或者叫第三方模块,python就强大在这里. 3.自定义模块. 标准库: 1.时间模块time与datetime ...

  5. Secure CRT修改文件夹的颜色

    secureCRT有一个很大的问题是,如果设置Emulation Terminal 为Linux模式,则ls的时候,目录的蓝色跟背景的黑色非常接近,很难看清楚,修改办法 option->Glob ...

  6. js 格式化相关的时间

    javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s). ...

  7. 小程序二维码生成接口API

    获取小程序码 我们推荐生成并使用小程序码,它具有更好的辨识度.目前有两个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口. 接口A: 适用于需要的码数量较少的业务场景 接口地址: http ...

  8. NetBeans配置Xdebug 远程调试PHP

    1.配置PHP 说明:xdebug.trace_output_dir和xdebug.profiler_output_dir需要增加权限 #chmod 755 /usr/xdebug-tmp xdebu ...

  9. PHP开发环境正确的错误信息处理

    正确记录配置 php.ini display_errors = On error_reporting = E_ALL log_errors = On error_log = F:/data/php/e ...

  10. MySQL备份还原之二使用mysqldump

    1 场景描述: create table gyj_t1(id int,name varchar(10)); insert into gyj_t1 values(1,'AAAAA'); commit; ...