A route named 'Home_default2' is already in the route collection. Route names must be unique.
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "area/{controller}/{action}/{id}",
defaults: new {area="Home_Default", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public override string AreaName
{
get
{
return "Home";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default2",
"Home/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
The auto generated code is bugging, what I did wrong?
To solve this problem, just delete all the .dll files in your bin folder and then build the solution again. This should solve the problem for you.
The problem is a duplicate `AreaRegistration.RegisterAllAreas(); on route and on global.asax
so need only this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Changing Home_Default to Home.
In my case, I had created an area & I had added this line in Route.config.
AreaRegistration.RegisterAllAreas();
But this statement was already present in Application_start of global.asax. hence got the error.
So removed it from route.config.
I did not change any route name for it. one route name was default (in RouteConfig file) & other was areaname_default (in AreaRegistration.cs file).
Related
I have an MVC 5 site with this structure:
example.com
example.com/area1 (index for whole section)
example.com/area2/item5 (specific item for section)
For SEO reasons I'd like to change every URL to this:
example.com/keyword (redirected from example.com)
example.com/keyword/area1
example.com/keyword/area2/item5
The keyword is fixed and always the same, though at some point in the future there may be a site with the same structure but different content with a different keyword in. That is probably at least a few months off though.
What is the quickest / easiest way to implement the above. Being able to get the name of keyword would be an advantage later though isn't essential right now.
I could use attribute routing, though I'd have to update A LOT of ActionMethods to do this - and most currently don't even use attribute routing.
thx.
Update
I've tried adding the below to RouteConfig.cs, but none of the Url's work for some reason:
routes.MapRoute(
name: "Default",
url: "keyword/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
string keyword = "keyword";
// keyword route - it will be used as a route of the first priority
routes.MapRoute(
name: "Defaultkeyword",
url: "{keyword}/{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
keyword = keyword
});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
also you need to add this Defaultkeyword route with few changes into your Area1AreaRegistration.cs file
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Area1_defaultKeyword",
"{Keyword}/Area1/{controller}/{action}/{id}",
new { Keyword = "Keyword", controller = "HomeArea1", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
I have some problems using Areas in my MVC project. I'm able to access my controller, which is located under the area, but when it returns to view (with model), I get an error:
The view 'Index' or its master was not found or no view engine
supports the searched locations.The following locations were searched:
~/Views/MyController/Index.aspx ~/Views/MyController/Index.ascx etc.
Here is the MyAreaAreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { controller = "MyController", action = "Index",
id = UrlParameter.Optional });
}
Routeconfig:
routes.MapRoute(
name: "Default", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyApp.Controllers" }
);
Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
...
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
And controller:
return View(myViewModel);
I'm totally stuck with this one. Any help would be much appreciated.
Please change your route config code .
routes.MapRoute(
name: "Default", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens.Add("area", "MyArea"); ;
It will be work for you .
Thanks .
I can not figure this out.
How to solve the problem?
AdminAreaReistration cs
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"CMSAdmin_default",
"CMSAdmin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
}
According to error image, you may use different namespaces when declaring an area into RegisterArea to avoid naming conflict between default route and area route:
AdminAreaRegistration.cs
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"CMSAdmin_default",
"CMSAdmin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "cms.site.Areas.CMSAdmin.Controllers" } // Insert area namespace here
);
}
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "cms.site.Controllers" } // Insert project namespace here
);
}
Possible causes from Multiple types were found that match the controller name error:
1) Using same controller name with different areas (this is likely your current issue),
2) Renaming project namespace/assembly name (delete old project name DLL file inside /bin directory then clean and rebuild again),
3) Conflict between references with same name but different versions (remove older reference then refactor).
References:
Multiple types were found that match the controller named 'Home'
Having issue with multiple controllers of the same name in my project
I created two areas (with controllers, views, registration, etc) with VS's scaffolding process. One for homecontroller and one called account.
I added a single line "route.DataTokens" to the routeconfig.cs file to connect to the homecontroller on startup.
var route = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional }
);
route.DataTokens["area"] = "Home";
Here is my HomeAreaRegistration.cs RegistrationArea method
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
But in the url of the browser I see
https://localhost:44301/Home/Home/Index
What I want to see is
https://localhost:44301/Home/Index
What do I modify to get this?
Edit - Solved! I think.
Here is what I did.
In HomeArearegistration.cs I removed the Home/
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Is this the proper way to go about doing this because something doesn't seem right here, if I type
https://localhost:44301/Home/Index.cshtml
which I would expect to take me to the home page, I get a HTTP 404 error not found but if I type
https://localhost:44301/Home/Index
It take me to the home page
I'm trying to add a route that shows some data based on a string parameter like this:
http://whatever.com/View/078x756
How do I create that simple route and where to put it?
In your global.asax.cs file, you add the following lines:
routes.mapRoute(
// The name of the new route
"NewRoute",
// The url pattern
"View/{id}",
// Defaulte route data
new { controller = "Home", action = "Index", id = "078x756" });
Make sure you add them before the registration of the default route - the ASP.NET MVC Framework will look throught the routes in order and take the first one that matches your url. Phil Haack's Routing Debugger is a valuable tool when troubleshooting this.
Routes are usually configured in the Application_Start method in Global.asax. For your particular case you could add a route before the Default one:
routes.MapRoute(
"Views",
"View/{id}",
new
{
controller = "somecontroller",
action = "someaction",
id = ""
}
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
controller = "home",
action = "index",
id = ""
}
);
Routes are added in the global.asax.cs
Example of adding a route:
namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"WhatEver"
"{View}/{id}",
new {controller = "Home","action = "Index", id="abcdef"}
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
}