ASP. NET MVC Routing Jin Jie (one)

Source: Internet
Author: User

1. WebForm page under the MVC framework.

We create a new WebForm page under the MVC project.

Then right-click to open the page, such as:

The discovery page will be accessible normally. OK, let's try to change the code in Global.asax.cs, as follows

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;usingSYSTEM.WEB.MVC;usingSystem.Web.Optimization;usingSystem.Web.Routing;namespacemvcmobiledms{//note:for instructions on enabling IIS6 or IIS7 Classic mode,//Visithttp://go.microsoft.com/?LinkId=9394801     Public classMvcApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas (); //GlobalFilters.Filters.Add (New MvcMobileDMS.App_Start.ActionAttributeFilter ());Webapiconfig.register (globalconfiguration.configuration);            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes); RouteTable.Routes.RouteExistingFiles=true;        Bundleconfig.registerbundles (Bundletable.bundles); }    }}

We added a code RouteTable.Routes.RouteExistingFiles = true; now, let's look at the page effect:

At this point, you can see the clues, yes, that is, the RouteTable.Routes.RouteExistingFiles property value determines the WebForm page of the route monitoring, the default is False, value is true

MVC's route monitoring masks WebForm pages and prompts for errors.

2. Use routing in WebForm.

In the WebForm project, if we are going to rewrite the address, we can use the address rewriting component method, but now under MVC we can try the following method:

Add the class file WebFormRouteHandler.cs with the following code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Routing;usingSystem.Web.Compilation;usingSystem.Web.UI;namespacedmswebapplication.app_start{ Public classWebformroutehandler:iroutehandler { PublicWebformroutehandler (stringvirtualpath) {             This. VirtualPath =virtualpath; }         Public stringvirtualpath {Get;Private Set; }  PublicIHttpHandler Gethttphandler (RequestContext requestcontext) {varpage = Buildmanager.createinstancefromvirtualpath (virtualpath,typeof(Page)) asIHttpHandler; returnpage; }    }}

Add the following code to the global application class Global.asax.cs:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Optimization;usingSystem.Web.Routing;usingSystem.Web.Security;usingdmswebapplication;namespacedmswebapplication{ Public classglobal:httpapplication {voidApplication_Start (Objectsender, EventArgs e) {            //Code that runs on application startupregisterroutes (routetable.routes);            Bundleconfig.registerbundles (Bundletable.bundles);        Authconfig.registeropenauth (); }         Public Static voidregisterroutes (routecollection routes) {routes. ADD ("Index",NewRoute ("Foo/bar",NewDMSWebApplication.App_Start.WebFormRouteHandler ("~/foo/haha.aspx"))); Routes. ADD ("Next",NewRoute ("Ha/yt",NewDMSWebApplication.App_Start.WebFormRouteHandler ("~/app/ye.aspx"))); }        voidApplication_End (Objectsender, EventArgs e) {            //Code that runs on application shutdown        }        voidApplication_Error (Objectsender, EventArgs e) {            //Code that runs if an unhandled error occurs        }    }}

When I enter the following address in the browser address bar, HTTP://LOCALHOST:34365/HA/YT, Access is http://localhost:34365/app/ye.aspx, such as:

When I enter the following address in the browser address bar, Http://localhost:34365/foo/bar, Access is http://localhost:34365/foo/haha.aspx, such as:

With this feature, we provide great convenience in the transition of traditional webform to the MVC architecture, and slowly transition to the MVC framework.

Well, it's written here today. Hope to be helpful to you O (∩_∩) o haha ~.

ASP. NET MVC Routing Jin Jie (one)

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.