How can I tell my mvc-application to route to a specific Controller and Action when they are not specified?
When debugging http://localhost:54500/ should route to http://localhost:54500/Home/Index.
Currently I have:
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
But this always throws
The view 'Index' or its master was not found or no view engine supports the searched locations
Edit #1
It should redirect/route to an View which resides in a Area called Home. Just want to clarify, that there is a Controller and an Area which both are named Home.
The config for the area is:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
When debugging http://localhost:54500/ should route to http://localhost:54500/Home/Index.
Actually, the way you have it configured, http://localhost:54500/ will route to the HomeController.Index method, not another URL.
The view 'Index' or its master was not found or no view engine supports the searched locations
This error indicates that routing succeeded, but the controller returned the path of a view that does not exist.
Since you also mentioned you are using an Area and have posted your configuration, it is clear what is happening. Your config is run in this order:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
So, if you pass the URL http://localhost:54500/, the Area route will miss (because it doesn't start with /Home) and it will match the Root route. This Root route does not route to your Area. There are 2 ways to fix this.
Option 1 - Add the Root Route to the Home Area
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index" }
);
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Option 2 - Set the DataToken to indicate the Home Area
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
).DataTokens["area"] = "Home";
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
In Core 1.0.1 you can do this in Startup.cs:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc(routes =>
{
routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}");
routes.MapRoute(
name: "default",
// template: "{controller=Home}/{action=Index}");
template: "{area=MyArea}/{controller=Home}/{action=Index}");
});
}
Related
in my application I have a CompanyGroupDataController.cs.
When the execution gets into its main action (Index), through this URL:.../CompanyGroupData/Index, the URL not longer ends in "/Index", so it erases the last part (althoug in the browser bar it keeps it). I think the problem is in my RouteConfig.cs which is this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{company}/{controller}/{action}/{id}",
defaults: new
{
company = Skill4IT.WebApps.Admin.Helpers.Tools.AdminUrlAccessCode,
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new string[] { "Skill4IT.WebApps.Admin.Controllers" }
);
routes.MapRoute(
name: "spa",
url: "#{controller}/{action}/{id}",
defaults: new
{
company = "admin",
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new string[] { "Skill4IT.WebApps.Admin.Controllers" }
);
routes.MapRoute(
name: "Catchall",
url: "{*catchall}",
defaults: new
{
controller = "Error",
action = "Error404",
}
);
Thanks
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 = "" }
);
}
I am trying to make a simple sample of passing string parameter in URL not using the queryString at this example. First of all I added a new MapRouteto RouteConfig.cs file as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "app",
url: "{controller}/{action}/{name}"
);
and in Controller I have
public class AppController : Controller
{
public string Index(string name)
{
return HttpUtility.HtmlEncode("Hello " + name );
}
}
but the view is not displaying the string parameter. For example a URL like this http://localhost:59013/App/Index/Ali only return Hello!
Why is this happening?
Update
routes.MapRoute(
name: "app",
url: "{controller}/{action}/{name}",
defaults: new {action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
First, you need to change the order of your registered routes so the default will be the last.
Secondly, your app route pattern is wrong and will always collide with the default route. You better change its pattern to
url: "App/Index/{name}"
Or perhaps to the more friendlier
url: "App/{name}"
Both with
defaults: new { controller = "App", action = "Index" }
So that your routes would look like:
routes.MapRoute(
name: "app",
url: "App/{name}",
defaults: new { controller = "App", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
See MSDN
Locally the website works fine, when deployed to azure we get the view index or its master was not found or no view engine supports the searched locations.
Routeconfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "websitename",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "{controller}", action = "{action}", id = UrlParameter.Optional }
);
We are using angularjs and having no problems until deployed to azure other than this issue.
You define following for home page right?
routes.MapRoute(
name: "DoransData",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Change it to
routes.MapRoute(
name: "DoransData",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Based on your code I think that you could only need following 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 = "Index", id = UrlParameter.Optional }
);
}
Hello by default ASP MVC routes are looks like:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
but is it possible to use it in next way with not optional ID:
routes.MapRoute(
name: "Default",
url: "{id}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = 0 }
);
Yes, it is possible.
Just tried:
RoutesRegistration:
routes.MapRoute(
"Default",
"{id}/{controller}/{action}",
new { controller = "Home", action = "Index", id = 0}
);
Controller:
public class HomeController : Controller
{
public ActionResult Index(int id)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
}