Reputation: 120508
I'm a bit rusty on my MVC routing.
I'm using a default route of
routes.MapRoute(
"Default",
"{controller}/{action}",
new
{
controller = "Default",
action = "Index",
});
I have a controller:
public class SomeController : Controller
{
public async Task<ActionResult> Index()
So, typically, this can be hit with either http://mydomain/Some/Index
-or- http://mydomain/Some
On this particular controller, I'd like to either deny (404) ~/Some/Index or simply redirect ~/Some/Index to ~/Some.
This is relatively easy to accomplish in the controller:
public async Task<ActionResult> Index()
{
var p = Request.Url.Segments;
var last = p.Last();
if(string.Equals(last, "index", StringComparison.InvariantCultureIgnoreCase))
{
return RedirectToActionPermanent("Index");
}
but now it seems like the controller is getting polluted by cross-cutting concerns. Is there a cleaner way?
Upvotes: 0
Views: 78
Reputation: 120508
So, if I add another route above the default route that dumps this specific url onto a non-existent controller/action, then I get a lovely 404. Perfect.
routes.MapRoute(
"SomeIndexDeny",
"Some/Index",
new
{
controller = "NonExistentController",
action = "NonExistentMethod"
});
routes.MapRoute(
"Default",
"{controller}/{action}",
new
{
controller = "Default",
action = "Index",
});
Upvotes: 0
Reputation: 17182
Yes there are much cleaner ways -
Option 1 - You can use HTtpModule for Permanant redirection
Option 2 - Use UrlRewriter
Option 3 - You can also implement an Action Filter in which you can redirect
Upvotes: 1