Modify the MVC view default search rule (iviewengine)

Source: Internet
Author: User

A few days ago I wrote a system, writing to the backend management system, I suddenly have an idea is: Want to create a new file in the regional view, processing the background of some business: area/adminmanager/view/content/ index.cshtml; So a path, I just built it up and found a bit wrong, because according to the MVC default rule is controllername/viewname, so I don't care how to create a folder, the view is under Managercontroller control, In other words, the address that arrives at the browser is this: Area/manager/index instead of our hearts: Area/adminmanager/content/index

Well, someone came up with a solution: that is to modify the routing;

Indeed, I have thought of this approach. I'm just going to modify the route in the area view:

context. MapRoute (  "managercontent",  "Manager/{controller }/{action}/{id}",  new"default", id= urlparameter.optional}  );

Then create a new controller in the controllers folder that specifically handles a business in the background--contentcontroller

Then run, the results are still exposed yellow pages, the contents of the display is 404, did not find the specified path under the view file, I read the contents of the yellow pages, is such

"XXX" view not found, ... The following location was searched: ~/area/adminmanage/views/content/welcome.cshtml~/area/adminmanage/views/content/welcome.vbhtml~/area/ adminmanage/views/shared/welcome.cshtml~/area/adminmanage/views/shared/welcome.vbhtml~/views/content/ Welcome.cshtml~/views/content/welcome.vbhtml~/views/shared/welcome.cshtml~/views/shared/welcome.vbhtml

After seeing this message, the idea in my mind is: How does MVC find these specified paths? Why don't you go find me. New ~/area/adminmanage/views/Manage/content/welcome.cshtml

I studied the rule because it is an area, so the area/adminmanage/views/in front of the search view is fixed, the content in the back content/welcome.cshtml is the controller name, Welcome is the action name

So that is the MVC framework default Lookup View path is this, only in the views root directory to find, and not to find its subdirectories, then how to modify this rule? MVC is not "convention better than configuration".

Must be related to the attempt to Iviewengine engine, I take questions to find information online, sure enough solution is still a lot of, the following I refer to the online approach:

< Summary > ///     Modify View engine Search view    file rules </summary>public    sealed class Multipleenginehelper:razorviewengine    {public        multipleenginehelper () {            viewlocationformats = new[]            {                "~/views/{1}/{0}.cshtml",                "~/views/shared/{0}.cshtml",                "~/areas/adminmanage/views/ Manage/{1}/{0}.cshtml ",            };        }        public override Viewengineresult Findview (ControllerContext controllercontext, String viewName, string mastername, bool UseCache)        {            return base. Findview (ControllerContext, ViewName, Mastername, UseCache);        }    }

It's useless to have this light, and you need to add a registration in Gloab

proteced void Application_Start () {     ....     Registerview ();} private void Registerview () {      ViewEngines.Engines.Clear ();      VIEWENGINES.ENGINES.ADD (New Multipleenginehelper ());}

So we're back to work!

Modify the MVC view default search rule (iviewengine)

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.