I have custom routing for controller added right over the default one:
Custom:
routes.MapRoute(
name: "FaqSubCategory",
url: "{culture}/{controller}/{action}/{TapCode}",
defaults: new { controller = "FAQ", action = "GetChosenFaqSubCategory", TapCode = UrlParameter.Optional },
constraints: new { culture = new CultureConstraint() }
);
Custom v2:
routes.MapRoute(
name: "FaqSubCategory",
url: "{culture}/{controller}/{action}/{TapCode}",
defaults: new { controller = "FAQ", action = "GetChosenFaqSubCategory", TapCode = UrlParameter.Optional },
constraints: new { culture = new CultureConstraint(), FAQ = new TranslateControllersConstraint("4189") }
);
Default:
routes.MapRoute(
name: "DefaultWithCulture",
url: "{culture}/{controller}/{action}/{ID}",
defaults: new { controller = "Home", action = "Index", ID = UrlParameter.Optional },
constraints: new { culture = new CultureConstraint() }
);
Basically because of that custom route, home page is receiving "/home/index" and some of the other pages are receiving "/index" at the end of the URL
When I write the custom routing like the second variant, then everything is working perfectly except the FAQ controller.
When an MVC application first starts it creates the route table.The default route table contains a single route named Default. So default route should have Default name, instead of DefaultWithCulture an your default route should have default culture
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{ID}",
defaults: new { culture="en", controller = "Home", action = "Index", ID = UrlParameter.Optional },
.....
Related
I have a default route value that is getting overridden by a custom one.
routes.MapRoute(name: "company-portal-program", url: "{companyName}/Programs/{programName}", defaults: new { controller = "Portal", action = "Program" });
routes.MapRoute(name: "company-portal", url: "{companyName}", defaults: new { controller = "Portal", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This is obviously because they overlap.
I would like the default route to take precedence and if the controller is not found the company-portal route should be used, is this possible?
I have a MVC application and can't quite get the routing working for my multi-tenant application. Here is the problem:
I have 2 types of pages in my application, most require the tenant name to be in the url but some don't. e.g.
These Do (tenant name is these examples is samsung and apple):
http://www.mytestapp.com/samsung/customers/add
http://www.mytestapp.com/apple/customers/add
These Don't:
http://www.mytestapp.com/home/register/
http://www.mytestapp.com/home/aboutus/
What routes do i require to get this working? I have tried this but it does not work for the register and about us page.
routes.MapRoute(
name: "TenantRoute",
url: "{tenantid}/{controller}/{action}/{id}",
defaults: new { tenantid = "tenantname", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Well, you'll need a second route to match the non-tenant routes. The default one should match if register and aboutus are controllers with an Index action:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Register", action = "Index", id = UrlParameter.Optional }
);
try with this
routes.MapRoute(
"samsung",
"samsung/{controller}/{action}/{id}",
new { controller = "YourController", action = "YourAction", id = UrlParameter.Optional}
);
routes.MapRoute(
"apple",
"apple/{controller}/{action}/{id}",
new { controller = "YourController", action = "YourAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Register", action = "Index", id = UrlParameter.Optional }
);
I have next map url definition where I want to set specific action and controller for special url:
//localhost:55321/SpecificPart/UserTextId all other routes should work in default way
but based on Route Debugger my URL is mapping to my main route rule how to handle this case ?
routes.MapRoute(
"PartnerSighUp",
"SpecificPart/{id}",
new { controller = "SpecificController ", action = "SpecificAction" },
new { id = "[A-Za-z].+" }
);
routes.MapRoute(
name: "EnglishHomePage",
url: "",
defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional }
);
Update if SpecificPart = SpecificController (controller name) it is working but if SpecificPart != SpecificController and looks to other controller it is not working
I have only default route enabled:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Which will resove paths like: hostname/Home/Index, hostname/Home/Foo, hostname/Home/Bar/23 just fine.
But I must also enable route like this: hostname/{id} which should point
to:
Controller: Home
Action: Index
{id}: Index action parameter "id"
Is such a route even possible?
If id is a number you could add a route above the other one and define a regex constraint:
routes.MapRoute(
name: "IdOnlyRoute",
url: "{id}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { id = "^[0-9]+$" }
);
Not Tested :
Create your route like this
routes.MapRoute(
name: "custom",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
under your default route
Since I'm short on time, I have configured route for every action. Luckly there are not many of them. :)
routes.MapRoute(
name: "IndexWithParam",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "IndexWithOutParam",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Login",
url: "Home/Login",
defaults: new { controller = "Home", action = "Login" }
);
So last route was repeated for ever other action. Not sure if it can be done better, but it works.
Have you tried to play around with AttributeRouting package?
Then your code will look like this
[GET("/{id}", IsAbsoluteUrl = true)]
public ActionResult Index(string id) { /* ... */ }
I'm adding a new route into my mvc web application and it's not working as desired. I was hoping someone could help me figure out where in the list it should go and maybe which defaults and/or constraints should be defined for it (in RouteConfig.cs).
The desired route would look like so:
/controller/id/slug/action e.g. mydomain.com/products/10/product-name/reviews
I've tried to define this route like so and have tried it as the 1st, 2nd and 3rd routes listed:
routes.MapRoute(
name: "AlternateRoute",
url: "{controller}/{id}/{slug}/{action}",
defaults: null,
constraints: new { id = #"\d+", slug = #"[\w\-\d+]*" }
);
What's happening is after I add the above route, and browse to a page like /products/10/product-name - url's that were previously something like /products/create look like /products/10/product-name/create (but only on that page).
The only other routes I have are these 3 (defined in my routeConfig file):
/controller/id/slug
routes.MapRoute(
name: "DefaultSlugRoute",
url: "{controller}/{id}/{slug}",
defaults: new { action = "Details", slug = "" },
constraints: new { id = #"\d+", slug = #"[\w\-\d+]*" }
);
/controller/action/year/month
routes.MapRoute(
name: "MonthlyArchiveRoute",
url: "{controller}/{action}/{year}/{month}",
defaults: new { controller = "Blog", action = "Archives", year = UrlParameter.Optional, month = UrlParameter.Optional },
constraints: new { year = #"\d{4}", month = #"\d{2}" }
);
/controller/action/id (the standard one included w/ a new mvc project)
routes.MapRoute(
name: "DefaultRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);