asp.net mvc5 伪静态 WebForm

时间:2022-01-01 13:58:44

Mvc4和5通用

1、背景:老项目WebForm开发 需要 融合到新项目Mvc5开发

2、需求:Url地址TruckDetail.aspx?id=455 达到效果 truck/455.html

3、不是最佳方案,但是个过度项目所以达到效果ok,欢迎丢转。

第一步:继承IRouteHandler

public class OldWebFormsRouteHandler : IRouteHandler
{
private string pageName = string.Empty;
private string id = string.Empty;
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{ pageName = requestContext.RouteData.GetRequiredString("oldPageName");
id = requestContext.RouteData.GetRequiredString("id"); string path="/Pages/" + this.pageName + ".aspx"; IHttpHandler MyHander = BuildManager.CreateInstanceFromVirtualPath(path, typeof(System.Web.UI.Page)) as IHttpHandler;
return MyHander;
}
}

第二步:在默认规则前ADD 注意.html

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.Add("MyRoute", new Route("{oldPageName}/{id}.html", new OldWebFormsRouteHandler())); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

第三步:修改Web.config

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

第四步:aspx页面获得参数Request.QueryString["id"]换成RouteData.Values["id"]

<form id="form1" runat="server">
<div> id:<%=RouteData.Values["id"]==null?"yy":RouteData.Values["id"].ToString() %><br />
oldPageName:<%=RouteData.Values["oldPageName"]==null?"no":RouteData.Values["oldPageName"].ToString() %> </div>
</form>

效果:

asp.net mvc5 伪静态 WebForm

转载请注明来源地址:http://www.cnblogs.com/huangyoum/p/4154338.html