简体   繁体   中英

My routing isn't working

I have a View folder structure like this:

Views
    Rooms
        Resorts
            Index.cshtml
            Suites.cshtml
            .....

I want the controller folder structure to match

Controllers
    Rooms
        ResortsController.cs

I added a new mapRoute

        routes.MapRoute(
            name: "Rooms",
            url: "Rooms/{controller}/{action}/{id}",
            defaults: new {controller = "Resorts", action = "Index", id = UrlParameter.Optional}
            );

But the controller for the View isn't being found. I have tried localhost/Rooms and localhost/Rooms/Resorts/Index -

both with the same result.

What am I missing?

EDIT: I need the URLS to look like Rooms/Resorts/Suites etc. per business requirements and I need to do this without using Areas. I will have multiple URLs that use the same Action name, like Rooms/Resorts/Suites, Rooms/Suites/Suites, Rooms/AwesomeSuites/Suites. So there is a point to using sub folders - to organize and to have more than one view named Suites.

Is what I need to do possible without using Areas?

Well, as far as I know the routing doesn't care about that extra folder (Rooms) that you created so it most likely tries to resolve the view from Views/Resorts/ and not from Views/Rooms/Resorts. If you need that kind of separation, like having a ResortsController somewhere else in the structure you should use Areas. You could for example create an Area named Rooms!

Can you try:

routes.MapRoute( "Rooms", "Rooms/Resorts/{id}", new {controller = "Resorts", action = "Index", id = UrlParameter.Optional} );

I solved my issue. Using my mapped route in my question, and keeping the folder structure intact that I implemented, I created a custom view engine that will dynamically create a path to look for the view.

public class CustomRazorViewEngine : RazorViewEngine
{
    public CustomRazorViewEngine()
    {
        ViewLocationFormats = new string[] { "~/Views/%1/{1}/{0}.cshtml"};
        MasterLocationFormats = new string[] { "~/Views/%1/{1}/{0}.cshtml",
                       "~/Views/Shared/%1/{0}.cshtml"};
        PartialViewLocationFormats = new string[] { "~/Views/Rooms/{1}/{0}.cshtml",
                        "~/Views/Shared/%1/{0}.cshtml"};
        FileExtensions = new string[] { "cshtml"};
    }

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
    {
        var path = GetPath(controllerContext.Controller.GetType().Namespace);
        return base.CreatePartialView(controllerContext, partialPath.Replace("%1", path));
    }

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        var path = GetPath(controllerContext.Controller.GetType().Namespace);
        return base.CreateView(controllerContext, viewPath.Replace("%1", path), masterPath.Replace("%1", path));
    }

    protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
    {
        var path = GetPath(controllerContext.Controller.GetType().Namespace);
        return base.FileExists(controllerContext, virtualPath.Replace("%1", path));
    }


    private string GetPath(string nameSpace)
    {
        var split = nameSpace.Split('.');
        int startingIndex = 0;
        StringBuilder sb = new StringBuilder();

        foreach(string s in split)
        {
            startingIndex++;

            if (s == "Controllers")
            {
                break;
            }
        }

        for(int x = startingIndex; x < split.Length; x++)
        {
            sb.Append(split[x]);
            if (x != split.Length - 1)
            {
                sb.Append("/");
            }
        }

        return sb.ToString();
    }

So as long as the folder structure is the same in the View folder and Controllers folder, the CustomRazorViewEngine will find the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM