0

Here is my .Net MVC Folder structure:

screenshot

I have a separate Folder called “Area” and inside that I have a Folder called “Restaurant”. Inside this “Restaurant” Folder I have a controller Called “MenuController” which has action named “Index”

I am tried to rewrite the (Custom Route Mapping) url inside “Global.asax.cs” using following code.

routes.MapRoute(
                "RestaurantMenu", // Route name
                "Restaurant/{id}", // URL with parameters
                new { controller = "/Restaurant/Menu", action = "Index", id = UrlParameter.Optional }
                // Parameter defaults
                );

But it gave me a HTTP 404 error.

1 Answer 1

2

The controller parameter inside your route should be the name of the controller, not the path :

If your controller name is Menu then, change it to this way :

routes.MapRoute(
    "RestaurantMenu", // Route name
    "Restaurant/{id}", // URL with parameters
    new { controller = "Menu", action = "Index", id = UrlParameter.Optional }
    // Parameter defaults
);

And the another strange thing is that : is this route sitting inside your Global.asax file? It should be inside your RestaurantAreaRegistration.cs file as follows;

public class RestaurantAreaRegistration : AreaRegistration {

    public override string AreaName {
        get {
            return "Restaurant";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context) {

        context.MapRoute(
              "RestaurantMenu", // Route name
              "Restaurant/{id}", // URL with parameters
              new { controller = "Menu", action = "Index", id = UrlParameter.Optional }
             // Parameter defaults
        );

        context.MapRoute(
            "Accommodation_default",
            "accomm/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );

    }
}

then you can give this a shot. If you would like to use ActionLink with this route, use it like that :

@Html.ActionLink("MyLink", "Index", "Menu", new { id = 1, Area = "Restaurant"})

I have written the above code with notepad so there might be some typos :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.