I am using this route to map all routes which were not found:
routes.MapRoute(
name: "NotFound",
template: "{*url}",
defaults: new { controller = "Home", action = "NotFound" }
);
The problem I encountered is that @Url.Action()
always returns null
on this route.
Could someone explain why is this happening and what could be alternatives to this?
After playing around, I found a "cheat" way to do it and it kinda works. If I get NotFound page then I redirect back to the same page if Url.Action() == null
if(this._urlService.Action() == null) //same urlHelper action, only has default Home page values passed into method
{
var query = string.Empty;
if(Request.QueryString.HasValue)
{
query = Request.QueryString.Value;
}
var path = "/Home/NotFound" + query;
path = string.Concat("/", this._userIdentity.CurrentLanguage.ToString().ToLower(), path);
return base.Redirect(path);
}
It could be because I use /{culture}/{Controller}/{Action}
as my main route. Creating other test project, where my main route is default /{Controller}/{Action}
has no problem at all finding the Url.Action()
on NotFound page