在ASP.NET WebForm简单实现伪静态,通过(System.Web.Routing)

开发环境:VS2010   

   VS2010已经集成了Routing组件,在ASP.NET MVC中,我们通过URLRouting实现了Controller,Action的URL控制。在WEBForm中,同样可以!马上开始。

首先,打开VS2010新建一个VS2010webForm,命名为(UrlTest),首先我们为网站添加System.Web.Routing引用,如图

在ASP.NET WebForm简单实现伪静态,通过(System.Web.Routing)_第1张图片

好,引用添加好了。

第二步,我们在Global.asax中添加,RouteCollection的注册,关键代码如下:

注:先Global.asax中添加一下引用<%@ Import Namespace="System.Web.Routing" %>

    
    
    
    
void RegisterRoutes(RouteCollection routes) { routes.MapPageRoute( " DefaultRoute " , " Default/{id}.html " , " ~/Default.aspx " ); } void Application_Start( object sender, EventArgs e) { RegisterRoutes(RouteTable.Routes); // 在应用程序启动时运行的代码 }

现在我们已经注册了一个Route,对Default/{id}.html,映射到Default.aspx,这样我们在Default.aspx只要通过RouteData对象,获取值,键为id的参数即可。

那么在Default.aspx.cs里面我们写入以下代码:

    
    
    
    
if (RouteData.Values[ " id " ] != null ) { Response.Write( " <h1> " + RouteData.Values[ " id " ].ToString() + " </h1> " ); }

好了,简单吧!运行程序输入Url  http://localhost:9801/UrlTest/Default/123.html

效果如图所示:

在ASP.NET WebForm简单实现伪静态,通过(System.Web.Routing)_第2张图片

看到URL地址了吧,还有页面左上角的”123“了吧。

 

当然要发布到IIS,记得配置下映射哦。

win7系统下IIS部署MVC项目 

首先打开IIS:
第一步:添加MVC程序映射

在ASP.NET WebForm简单实现伪静态,通过(System.Web.Routing)_第3张图片
打开其中的:处理程序映射,如下图:


在ASP.NET WebForm简单实现伪静态,通过(System.Web.Routing)_第4张图片

点击界面右边操作中的:添加脚本映射,弹出下图:

在ASP.NET WebForm简单实现伪静态,通过(System.Web.Routing)_第5张图片

请求路径:*           可执行文件:c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll    名称:MVC       点击确定;

第二步:添加应用程序池

在ASP.NET WebForm简单实现伪静态,通过(System.Web.Routing)_第6张图片

填写应用程序池名称及版本,托管管道模式设置为经典;应用城池添加完成
接着在新建的站点中选择刚建的应用程序池即可

 

IIS Unrecognized attribute 'targetFramework' 错误解决方案

在配置IIS服务器网站是遇到下面的问题:

Configuration Error

Description:An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.

Source Error:

Line 10:             during development.
Line 11:         -->
Line 12:   <compilation debug="false" targetFramework="4.0">
Line 13:   </compilation>
Line 14:   <!--

解决方案:

1. 选中你要发布的网站

2. 选择右边的基本设置.

3. 在出现的编辑网站对话框中选中“选择” 按钮

4. 在出现的选择应用池程序对话框中选择 ASP.NET v4.0

5. 测试OK

示意图:

在ASP.NET WebForm简单实现伪静态,通过(System.Web.Routing)_第7张图片

调用路径:http://localhost:82/default/123.html(记得加上.HTML哦)

后台调用,使用传递的参数:

 

RouteData.Values["id"].ToString()

我的global文件

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);
           
        }
        void RegisterRoutes(RouteCollection routes) 
        { 
            routes.MapPageRoute("DefaultRoute", 
            "Default/{id}.html", "~/Default.aspx");
        } 

我的 CONFIG代码

<configuration>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>

    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
    <httpModules>
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />      
    </httpModules>
    <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="UrlRoutingModule"/>
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <handlers>
      <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web,Version=2.0.0.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>

</configuration>


 

你可能感兴趣的:(mvc,application,webform,asp.net,IIS,compilation)