ASP. MVC5 Pseudo-static

Source: Internet
Author: User

ASP. MVC5 Pseudo-Static WebForm

Mvc4 and 5 Universal

1, background: The old project WebForm development needs to integrate into the new project MVC5 development

2, demand: URL address truckdetail.aspx?id=455 reach the effect truck/455.html

3, not the best solution, but an over-project so to achieve the effect OK, welcome to lose.

First step: Inherit 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;        }    }

Step Two: Add note before the default rule. 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}            );        }    }

Step Three: Modify the Web. config

<system.webServer>    <modules runallmanagedmodulesforallrequests= "true"/></system.webserver >

Fourth step: aspx page get parameter request.querystring["id" replaced with 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>

Effect:

Reprint please indicate source address: http://www.cnblogs.com/huangyoum/p/4154338.html

ASP. MVC5 Pseudo-static

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.