What should I do in the MVC controller Drop directory?

Source: Internet
Author: User

Method One, you can name a controller in the directory with the same name as the directory, and then use the features to set up, such as:

Controllers/myfolder/myfoldercontroller.cs

I built the above structure, note that MyFolder is my own establishment of the directory, so the controller name should also be handed myfolder.
Then write in the controller:

[HttpGet]
[ActionName ("Index")]
Public ActionResult ABCD (string id) {
Retrun View ("~/views/home/about.cshtml");
}






MVC multilevel Views Directory ASP. MVC4 routing rewrite and modify the rules of view's look-up in general we will encounter this problem in the MVC development process. The page is always written under the Views folder, and only a controller's page can be written in the corresponding folder named after the controller name. What if we write somewhere else? Then there will be an error. This is an established rule in MVC and must be written like this.
1. Normal project directory, such as:
      

To access the index page, we only need to enter Home/index to access it. The reason we are able to access this is because we have a default route configured by default at the beginning of project creation. We can follow this default routing rule for access.



2. Then we'll look at the way we need access, such as


If we want to access the admin under the TestController Index page, then we enter the Test/index, this is definitely not. Because TestController is not at all in the root directory of controllers, but under the controllers/admin, so we can not find the controller of test. Then we enter Admin/test/index, then we need to add a route configuration, because the previous default route can only be accessed through {Controller}/{action}/{id}, that is, it must start with a Controller. The routes we reconfigure are as follows:



public static void RegisterRoutes (RouteCollection routes)
{
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");

Routing rule matching is from top to bottom, and priority matching routes must be written at the top. Since the route match succeeds, he will not continue to match.
Routes. MapRoute (
"Admin",//route name, as long as it is guaranteed to be unique in the routing set
"Admin/{controller}/{action}/{id}",//routing rules, matching URLs beginning with Admin
New {controller = "Home", action = "Index", id = urlparameter.optional}//
);

Routes. MapRoute (
"Default",//Route name
' {controller}/{action}/{id} ',//URL with parameters
New {controller = "Home", action = "Index", id = urlparameter.optional}//Parameter defaults
);

}
View Code So when we enter Admin/test/index again this time, can we find views/admin/test/index.cshtml this page? Obviously not, because in addition to the routing configuration how to access the controller, the search for views inside the page also has its own rules. The test result is definitely not find the page, we look at the error message to know how he looked for the cshtml page.

    

    

"The following locations were searched" translates to "the following addresses have been searched". So he only searches for these addresses, and here I only write the address of the Razor view, which is written as a wildcard character:
views/{1}/{0}.cshtml
views/shared/{0}.cshtml
{1} represents the name of the controller, {0} represents the view name, and shared is the folder where the template page is stored. It's clear at first sight. This is the rule of looking for a view, so the storage rules we store in admin/test/index.cshtml are not satisfied. Then we modify the following, such as:



Put the Test folder directly under the views, then we will satisfy the rule of looking for the view, we entered Admin/test/index, and indeed the visit was successful.

    

However, the storage of this method is certainly not what we need, since our controller distinguishes the storage, we certainly hope that views can also be stored in this way.

3. Then we go to the point and modify his view-seeking rules so that he can access them according to our rules, just like modifying routes.
Create a new CS class myviewengine in the project and inherit the Razorviewengine. The code is as follows:



Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Namespace Mvcroute.mvcex
{

public sealed class Myviewengine:razorviewengine
{

Public Myviewengine ()
{
Viewlocationformats = new[]
{
"~/views/{1}/{0}.cshtml",
"~/views/shared/{0}.cshtml",
"~/views/admin/{1}/{0}.cshtml"//Our Rules
};
}
public override Viewengineresult Findview (ControllerContext controllercontext, String viewName, string mastername, bool UseCache)
{
Return base. Findview (ControllerContext, ViewName, Mastername, UseCache);
}

}
}
View Code then registers the rule with the system and registers it in global so that we can access it in our own way. Global registration is as follows:



protected void Application_Start ()
{
Arearegistration.registerallareas ();

Registerglobalfilters (globalfilters.filters);
RegisterRoutes (routetable.routes);

Registerview ();//Register View access rules
}

protected void Registerview ()
{
ViewEngines.Engines.Clear ();
VIEWENGINES.ENGINES.ADD (New Myviewengine ());
}
View Code results such as:

    





What should I do in the MVC controller Drop directory?

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.