MVC static routing

    mvc usually written standard (http: // localhost: 8149 / Home / Index) routing configuration as follows:

    Sometimes demand such as http: // localhost: 8149 / Home / Index changed to http: // localhost: 8149 / index.html makes it look more like a static website

   

1
2
//配置首页 伪静态 路由
      routes.MapRoute( "pc_index" "index.html" new  { controller =  "Home" , action =  "Index"  });

 however  

 

A solution (not recommended) Modify the Web.config file  

1
2
3
4
<system.webServer>
     <modules runAllManagedModulesForAllRequests= "true"  >
</modules>
</system.webServer>

Strongly not recommended in this way: 
1, in the form of these problems is to run all registered HTTP module on each request, rather than hosting request (e.g. .html). This means that the module will run forever .jpg .gif .css .aspx, etc.
2, waste of resources
3, and has a global effect may lead to wrong

A better solution (second approach)

1
2
3
4
5
6
<system.webServer>
     <modules >
       <remove name= "UrlRoutingModule-4.0"  />
       <add name= "UrlRoutingModule-4.0"  type= "System.Web.Routing.UrlRoutingModule"  preCondition= ""  />
     </modules>
   </system.webServer>

 

A better solution (third approach)

1
2
3
4
5
<system.webServer>
    <handlers>
      <add  name= "htmlHandler"  verb= "GET,HEAD"  path= "*.html"  type= "System.Web.StaticFileHandler" />
    </handlers>
  </system.webServer>
 
 

 

 

mvc routing parameters to obtain a special way, in different places, obtaining routing parameters are not the same, respectively, said here about how to obtain routing parameters in the controller, the non-controller inside the class, and in the view:

 

1. Get in the controller routing parameters:

1
var  controller = RouteData.Values[ "controller" ]; //action,id或其他路由参数同理

 2. Get in the view:

1
<input type= "text"  value= "@Html.ViewContext.RouteData.Values[" controller "]"  />

3. In the non-class controller:

1
HttpContext.Current.Request.RequestContext.RouteData.Values[ "controller" ]

Guess you like

Origin www.cnblogs.com/lg1010/p/12155520.html