VI. Routing rules modification of ASP. NET MVC4.0 Development CMS system case

Source: Internet
Author: User

Open today, I found that if I were to build the view page csthtml to create multiple folder directories, Contraller would not be found. In actual development work, it is not possible for all generated views to be placed under the one-level folder under View, for the system's code management is not very scientific, so I try to make it happen.

For example, the login function, the controller set up the path is: Contrallers/sys/com/usercontraller.cs.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/0F/wKioL1R23A_yLhAbAABMbwMWnrU188.jpg "title=" Qq20141127160614.jpg "width=" 303 "height=" "border=" 1 "hspace=" 0 "vspace=" 0 "style=" WIDTH:303PX;HEIGHT:114PX; "alt = "Wkiol1r23a_ylhabaabmbwmwnru188.jpg"/>

System Add view default generated view path is: view/user/userlogin.cshtml. If I want to change the path to view/sys/com/ Userlogin.cshtml, and Contraller to keep, will be the error system can not find it.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/54/11/wKiom1R23NHCY4myAAJlK7u6Cu4900.jpg "title=" Qq20141127160614.jpg "alt=" Wkiom1r23nhcy4myaajlk7u6cu4900.jpg "/>

The system default rules will only be searched in view/user/. This is because the route sets the/view/{contraller}/{action}/id rule and is not found when the file is moved. So we need to tell where the real location is.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/54/11/wKiom1R234uReXL5AABpUqWssDc197.jpg "title=" Qq20141127162312.jpg "alt=" Wkiom1r234urexl5aabpuqwssdc197.jpg "/>

Custom routing rules, create a new class Myroute under the App_start folder. Inherits from Razorviewengine overrides the selection view method Findview. Write the following code:

Using system;using system.collections.generic;using system.linq;using system.web;using  system.web.mvc;namespace hillstone.app_start{    public sealed class  MyRoute : RazorViewEngine    {         /// <summary>        ///  Custom Routing Rules          /// </summary>         Public myroute ()         {             ViewLocationFormats = new[]             {                  "~/views/{1}/{0}.cshtml",                  "~/views/shared/{0}.cshtml",                  "~/views/sys/com/{1}/{0}.cshtml"              };        }         public override viewengineresult findview (Controllercontext controllercontext,  string viewname, string mastername, bool usecache)          {            return  Base. Findview (Controllercontext, viewname, mastername, usecache);         }    }}

The

Allows this routing rule to be registered in global, with the following registration method:

Using system;using system.collections.generic;using system.linq;using system.web;using  System.Web.Http;using System.Web.Mvc;using System.Web.Optimization;using  system.web.routing;using hillstone.dal;using hillstone.app_start;namespace hillstone{     //  Note:  For instructions on enabling  IIS6  or  IIS7  Classic mode,     //   Please visit  http://go.microsoft.com/? linkid=9394801    public class mvcapplication :  system.web.httpapplication    {        protected  void application_start ()         {             arearegistration.registerallareas ();             webapiconfig.register (globalconfiguration.configuration);      &Nbsp;      filterconfig.registerglobalfilters (GlobalFilters.Filters);             routeconfig.registerroutes (RouteTable.Routes );             bundleconfig.registerbundles ( Bundletable.bundles);             registerview ();// Registering View access rules            //  System.data.entity.database.setinitializer

Run OK.

This article is from the "Run small snail-original space" blog, please be sure to keep this source http://songjiahao.blog.51cto.com/4433831/1583385

VI. Routing rules modification of ASP. NET MVC4.0 Development CMS system case

Related Article

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.