V
V
Viktor Korzunin2015-05-25 07:50:06
ASP.NET
Viktor Korzunin, 2015-05-25 07:50:06

Customizing Url Parameter Routing with ASP.NET MVC Default Action?

Hello @user.Name
I have the following RouteConfig method:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Catalog",
                url: "Catalog/{category}",
                defaults: new { controller = "Catalog", action = "Index" }
            );

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

method of the CatalogController class:
public ActionResult Index(string category)
        {
            return View(category);
        }

and view ~/Views/Catalog/Index.cshtml
@model string
@{
    ViewBag.Title = Model;
}
@Model

I'm trying to access http:/localhost:51125/Catalog/Test , and I'm expecting to see a view with a Test parameter passed in parameter passed to the view model, but I see in response
Server error in application '/'.
The view 'Test' or its instance could not be found, or no view engine supports search locations. The following locations were searched:
~/Views/Catalog/Test.aspx
~/Views/Catalog/Test.ascx
~/Views/Shared/Test.aspx
~/Views/Shared/Test.ascx
~/Views/Catalog/Test. cshtml
~/Views/Catalog/Test.vbhtml
~/Views/Shared/Test.cshtml
~/Views/Shared/Test.vbhtml

Please tell me what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-05-25
@F1oyd

category in this case is taken as the name of the view because of the overload, resulting in object return View(category as object);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question