Routing with dashes in ASP.NET MVC

I am working on an application with ASP.NET MVC 5. I want my application to have a route that looks like this:


Notice how the route has a dash (-) in it. I currently have a controller named MyCategoryController. It is defined as follows:

namespace MyApp.Controllers
    public class MyCategoryController : Controller
        // GET: List
        public ActionResult Index()
            return View();


The view is in /Views/My-Category/Index.cshtm

l. When I try to access

in the browser, I get the error:

Resource is not found.

I set a breakpoint and I noticed that the breakpoint was missing. Then I enter into the browser and I get the error:

The Index view or its master was not found, or the view engine does not support the found locations. The following locations were searched:

~ / Views / my_category / Index.cshtml
    ~ / Views / my_category / Index.vbhtml
    ~ / Views / Shared / Index.cshtml
    ~ / Views / Shared / Index.vbhtml

How do I setup ASP.NET MVC so that a) I can visit

b) Load a view from/Views/my-category/Index.cshtml


source to share

1 answer

You need to specify the views folder as a controller, not like a route.

So /Views/MyCategory/Index.cshtml

, not /Views/My-Category/Index.cshtml


If for some reason you have no idea why, you want it to be /Views/My-Category/Index.cshtml

, you need to "completely style the view":

return View("~/Views/My-Category/Index.cshtml");


About the dash route: I don't use attribute based routing, so I can only guess:
Have you added routes.MapMvcAttributeRoutes();

to your method RegisterRoutes


route is routed by default "{controller}/{action}/{id}"




All Articles