Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to make "controller-less" urls with ASP.NET MVC?

I'm using ASP.NET MVC 4, .NET 4.5. Any way, besides creating individual controllers for each "action", to have "controller-less" urls?

What I mean is, have a Home controller filled with actions. Urls such as:

  • site.com/Home/About
  • site.com/Home/Contact

to become

  • site.com/About
  • site.com/Contact

but still use the Home controller.

like image 871
Chaddeus Avatar asked Feb 01 '13 14:02

Chaddeus


People also ask

Can we have multiple views for single controller?

You can / will absolutely have multiple Views from a Controller. Just think about creating a model for each view if you want to stick up with MVC pattern. Save this answer.

What should controller do in MVC?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.


1 Answers

You can define routes that don't contain the controller name like this:

routes.MapRoute(
    "About",                                       // Route name
    "About/",                                      // URL with parameters
    new { controller = "Home", action = "About" }  // Parameter defaults
);

routes.MapRoute(
    "Contact",                                       // Route name
    "Contact/",                                      // URL with parameters
    new { controller = "Home", action = "Contact" }  // Parameter defaults
);
like image 178
david.s Avatar answered Sep 28 '22 02:09

david.s