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

开发环境:VS2010   

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

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

好,引用添加好了。

第二步,我们在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

效果如图所示:

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

 

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

win7系统下IIS部署MVC项目 

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

win7系统下IIS部署MVC项目 - Eval___orz - ——————
打开其中的:处理程序映射,如下图:


win7系统下IIS部署MVC项目 - Eval___orz - ——————

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

win7系统下IIS部署MVC项目 - Eval___orz - ——————

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

第二步:添加应用程序池

win7系统下IIS部署MVC项目 - Eval___orz - ——————

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

 

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

示意图:


调用路径: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>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值