ASP MVC Route not firing - asp.net

I am having a bit of trouble getting my routing working correctly. I am needing to support IIS 5.1, so I want to suffix the controller with .mvc, here is what I have currently:
routes.MapRoute(
name: "Default",
url: "{controller}.mvc/{action}",
defaults: new { controller = "App", action = "Home" }
);
This fails. If I remove the .mvc, it works as expected. Also, weirdly enough if I add another path in between the controller and action, it works. Like this:
routes.MapRoute(
name: "Default",
url: "{controller}.mvc/Anything/{action}",
defaults: new { controller = "App", action = "Home" }
);
Does anyone know the reason for this?
Thanks in advance,
Ryan.

I have tried with code as below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DefaultWithExtension",
url: "{controller}.mvc/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
It works correctly. Please try those code in your application.

IIS could be treating the request as a static file or another handler is receiving and processing the request.
If this is the case, you'll need to add a handler mapping for the .mvc extension in IIS and ensure it's using ASP.NET.

Related

asp.net mvc 5 can i hide controller name from url

we have one project in asp.net mvc 5 when i run my project and click on some button for go to the next page then we are getting the url like
wwww.abx.com/project/projectname .
where project is my controller name and projectname is Action name
so if i want so url like
wwww.abx.com/projectname
then this is possible ?
please if any one have some solution so please give it
here is my route.config file code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "view1", id = UrlParameter.Optional }
);
}
Thanks
you have to create a new route:
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ProjectName1",
url: "Projectname1",
defaults: new { controller = "Project1", action = "Projectname1" }
);
routes.MapRoute(
name: "ProjectName2",
url: "Projectname2",
defaults: new { controller = "Project2", action = "Projectname2" }
);
routes.MapRoute(
name: "ProjectName3",
url: "Projectname3",
defaults: new { controller = "Project3", action = "Projectname3" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "view1", id =
UrlParameter.Optional }
);
}
If you would like to do this for only one controller - add a custom route as suggested by #Hamed Javaheri, or use attribute mapping as described here
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Project",
url: "projectname",
defaults: new { controller = "project", action = "projectname", id = UrlParameter.Optional }
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
If you want this to work for many / most routes then you might want to change the default mapping, somethin similar to this question:
ASP.NET MVC Default URL View and
ASP.NET MVC Routing with Default Controller
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{action}",
defaults: new { controller = "project", id = "" }
);
}

multiple routes with same url

I currently have the following in my RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Identity",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity" }
);
I'm trying to use the following in a Action method:
return RedirectToRoute("Identity", new { #id = id });
but it doesn't seem to go there, when I check with Fiddler, i see the request is going back to the same page i was currently on, it appears to be hitting the Default route. Is there anyway to force it to get the Identity one, even if their urls are the same, I want to use that to force users to the other controller when needed.
MVC will stop looking for a route as soon as a match is found. In your case, it will look in the following order: 1.) Default 2.) Identity.
If you want to create a specific route with the same pattern, you can achieve it with the following code:
routes.MapRoute(
name: "Identity",
url: "Profile/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You do not need to 'Force' the user to another controller. It is a good practice to use the RedirectToAction method so you can supply the controller and action you want to bring the user to.
The solution supplied by #thiag0 will not work.
Use the following
return RedirectToAction("Identity", "Profile", new { id = 5 });
and in your Profile controller, make sure you can accept the parameter
public ActionResult Identity(int id)
{
return View();
}
and in RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Identity",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity"}
);

How to redirect some actions to another controller in mvc routes?

I have mvc actions defined in the controllers TasksController and TasksGridViewController. If the URL Tasks/Index or Tasks/GridView is called, MVC should look up the action in the TasksGridViewController, for all other actions it should use the TasksController.
I tried the following route for the action Index, but it doesn't work:
routes.MapRoute(
name: "GridViewController/Index",
url: "{controller}/Index",
defaults: new
{
controller = "{controller}GridView",
action = "Index"
});
What am I doing wrong here? Thanks for any help.
You could define those 2 specific routes BEFORE the default route:
routes.MapRoute(
name: "GridViewController_Index_Action",
url: "Tasks/Index",
defaults: new
{
controller = "TasksGridView",
action = "Index"
}
);
routes.MapRoute(
name: "GridViewController_GridView_Action",
url: "Tasks/GridView",
defaults: new
{
controller = "TasksGridView",
action = "GridView"
}
);
// Now comes the default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "" }
);

Asp.Net MVC Routing in page load

On my RouteConfig, I have Controller and Action name placed on the Default Route.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Controller", action = "Index", id = UrlParameter.Optional }
);
While in page load, now the path is just 'http://geomig.com/'.
Is it possible to display full path, means 'http://geomig.com/Controller/Index' on page load.
Please help me.
You need rewrite the path using RewritePath() method.
Use the following link to know how RewritePath() works:
RewritePath
Example:
string originalPath = HttpContext.Current.Request.Path.ToLower();
if (originalPath == "/")
Context.RewritePath("/Home/Index");
Remove the controller and action defaults. Simply have
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);

ASP.NET WebPages Routing

I created simple asp.net project.
Have folder Account, and many files like Register.cshtml, LogIn, etc...
And url: localhost/Account/Register
Want it to be: localhost/Register, localhost/LogIn.
Tried to map it in _AppStart.cshtml:
RouteTable.Routes.MapPageRoute("Account", "{Action}", "~/Account/{Action}");
And it doesn't work.
First of all you have to use MapRoute to MapPageRoute, because MapPageRoute is (from msdn):
Provides a way o define routes for Web Forms applications
But if you modify your code like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("AccountRoute", "{Action}", new {controller = "Account"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Every request without parameters will go to AccountRoute, even if it should be handled by other controler.
The only way it will be working is to create code like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//ALL routes you need
routes.MapRoute("LoginRoute", "{Action}", new { controller = "Account", action = "Login" });
routes.MapRoute("RegisterRoute", "{Action}", new { controller = "Account", action = "Register" });
//Default route in the end
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Try this one....
//Default
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"",
new { controller = "Home", action = "Index", id = "" }
);
//url rewriting
RouteTable.Routes.MapRoute(null, "Register/{ID}", new { controller = "Home", action = "Register" });

Resources