I'm new to ASP.Net Mvc and I'm so confused about routing configurations. After I deployed my project to production requirements changed about urls. First version of my application my routing configuration was like below
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
But customer wanted to access application like "http://{serveraddress}/ClaimFilePortal/{controller}/{action}/{id}". After this I have changed my routing config like below.
routes.MapRoute(
name: "Default",
url: "{applicationname}/{controller}/{action}/{id}",
defaults: new { applicationname ="ClaimFilePortal",controller = "Account", action = "Index", id = UrlParameter.Optional },
constraints:new {applicationname = "ClaimFilePortal"}
);
But in controllers' code there are redirections like and mvc not adding "ClaimFilePortal" constraint.
return RedirectToAction("Index2", "Account",
new RouteValueDictionary(new { systemUserId = systemUserId, returnUrl = "/Upload/RedirectToFolder" + Request.Url.Query}));
So, how can I add a configuration for all redirections (Redirect(), RedirectToAction(), RedirectToRoute() etc.) to add my constraints before all urls.
To add 'ClaimFilePortal' as default prefix, remove the {} from it and try like:
routes.MapRoute(
name: "Default",
url: "applicationname/{controller}/{action}/{id}",
defaults: new { applicationname ="ClaimFilePortal",controller = "Account", action = "Index", id = UrlParameter.Optional },
constraints:new {applicationname = "ClaimFilePortal"}
);
Related
Let say I have a website www.example.com
the default routing looks like
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
Ok that works fine but let's say I want my site when I go to www.example.com/id to go to www.example.com/login/index/id
How would I configure/add routing for this, without breaking my other pages where I am actually trying to go to www.example.com/controller?
EDIT: Unfortunately id is a string so I do not have any concrete constraints that I can think of that would work. Think of maybe instead of the id I should have said companyname or sitename so the URL would look like www.example.com/companyname .
The only solution that I have come up with so far is adding a maproute for each one of my controllers like this
routes.MapRoute(
name: "Home",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Settings",
url: "Settings/{action}/{id}",
defaults: new { controller = "Settings", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "companyname",
url: "{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
This will work but I have many controllers and if I add one in the future and forget to adjust the routes it will fail. Also, this is unlikely but if a companyname happens to the be same as one of my controller names it would also fail.
In controller you may redirect to another Controller/action:
public ActionResult yourAction()
{
return RedirectToAction("nameAction","nameController");
}
Did you tried adding this mapping first:
routes.MapRoute( name: "Custom", url: "{id}", defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional } );
That should work but keep in mind that routes are evaluated secuentially, so you will have to organize mappings in order to reach out all pages in your site.
For example, routes like www.example.com/Product could be redirected to /Login by mistake.
EDIT: You can add constraints, so if id is an int value, you can try with the following:
routes.MapRoute("Custom", "{id}",
new { controller = "Login", action = "Index" },
new { id = #"\d+" }
EDIT 2: Having ids as string values, the only solution I see is to manually add each controller as you said, or to add something like this:
routes.MapRoute(
name: "Default",
url: "app/{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
This way you don't need to update each route in the future.
Please try below routing
routes.MapRoute(name: "companylogin", url: "companylogin/{id}", defaults: new
{
controller = "Login",
action = "Index",
id = UrlParameter.Optional
});
routes.MapRoute(name: "default", url: "{controller}/{action}/{id}", defaults: new
{
controller = "Login",
action = "Index",
id = UrlParameter.Optional
});
Remove other controller specific routing. Now you can navigate to login using
url : - www.example.com/companylogin/{id} and all other url redirect default route.
I would like to revise the questions here, my focus is on MVC Url.Action:
I am testing on Url.Action in view http:// localhost:22334/Order/Index
#Url.Action("Summary", "Order")
When the route config is
routes.MapRoute(
name: "Order",
url: "{lang}/Order/{action}",
defaults: new { lang = UrlParameter.Optional, controller = "Order", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{*pathInfo}",
defaults: new { controller = "Order", action = "Index", id = UrlParameter.Optional }
);
It generate a link //Order/Summary which is not working
However when the Route Config is added with constrait
routes.MapRoute(
name: "Order",
url: "{lang}/Order/{action}",
defaults: new { lang = UrlParameter.Optional, controller = "Order", action = "Index" },
constraints: new { lang = "[a-z]{2}" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{*pathInfo}",
defaults: new { controller = "Order", action = "Index", id = UrlParameter.Optional }
);
It generate a link /Order/Summary which works well
Is URL.Action depends on the routing?
Since I am testing on localhost, will URL.Action generate different result when I put on server like 'http://myserver/myapps/ordersystem/Order/Index
Thanks
Best Regards,
mintssoul
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 the following:
routes.MapRoute(
"Account",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
However I don't want to specify a default action. Is there a way to do this for my MVC5 application ?
Of course you can.
routes.MapRoute(
"Account",
url: "Account/{action}/{id}",
defaults: new { controller = "Account", id = UrlParameter.Optional }
);
I have set up the following structure for my website. (see link below)
http://postimg.org/image/ut4klihrp/
localhost:62540 Goes to the index page of the core
localhost:62540/www/Home Goes to the index page of the WWW
localhost:62540/cms/Home Goes to the indec page of the cms
I basically want the 'default' route (localhost:62540) to go to my WWW project. How can i do this or does anybody know a tutorial were the principle for this get explained? Or is this not possible since i use the area method.
Eventually i want to remove the view and controller from the core project.
Route config in www :
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);
You can specify the default area in the defaults:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = "www", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);