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) { /* ... */ }
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.
Is there a way to define route like this
routes.MapRoute(
name: "Language",
url: "{controller}/{action}.{culture}",
defaults: new { controller = "Home", action = "Index", culture = UrlParameter.Optional }
);
And be able to handle url like http://www.domain.com/Test/Create.us-US?
Yes, you can do it, but add route for url like http://www.domain.com/Test/Create without dot at the end
routes.MapRoute(
name: "Language",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", culture = "us-US" }
);
No you can create route without with dot but you use the special caracter to the root. When we declare the method at the controler at that time you can specify the Action name define in the route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "DashboardV1_bm", id = UrlParameter.Optional }
);
[HttpPost, ActionName("DashboardV1_bm")]
[ValidateAntiForgeryToken]
public ActionResult DashboardV1(int id)
{
Shift shift = db.Shifts.Find(id);
db.Shifts.Remove(shift);
db.SaveChanges();
this.AddToastMessage("Delete", "Record is successfully deleted !", ToastType.Success);
return RedirectToAction("Index");
}
For more information, please visit my blog:
http://programmingcode.in/create-routemap-to-access-new-created-view-page-in-mvc-dot-net/
Defined a route:
routes.MapRoute(
name: "SearchRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Search",
action = "Index", id = UrlParameter.Optional }
Index method:
public ActionResult Index(int? id, SearchViewModel model)
I browse to http://xxx.xxx.xxx.xxx/Search and my Index action is
hit on my SearchController, good.
I browse to http://xxx.xxx.xxx.xxx/Search/1 and I get a 404 error.
I browse to http://xxx.xxx.xxx.xxx/Search/Index/1, OK.
I'd rather work with 2 above and not have to type in /Index/1. How?
You can create your route like this:
routes.MapRoute(
name: "SearchRoute",
url: "{controller}/{id}",
defaults: new { controller = "Search", action = "Index", id = UrlParameter.Optional }
);
It will only ever target the Index action but it sounds like what you want.
There is priority in routing, ie: which route you define first matters. Try following:
routes.MapRoute(
name: "SearchRoute",
url: "Search/{id}",
defaults: new { controller = "Search",
action = "Index", id = UrlParameter.Optional }
routes.MapRoute(
name: "DefaultRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
I have the following route:
routes.MapRoute(
name: "Default",
url: "home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This works fine and directs to the home controller Index action where the user enters /home
Is there a way that I can ALWAYS make it direct to the Index action if the user enters:
/home/xxx or
/home/xxx/xxx
Where xxx can be anything?
Sure, just remove the {action} part from your route definition so that the action is always Index:
routes.MapRoute(
name: "Default",
url: "home/{anything}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If you remove the {action} from the url and keep it as a default value then it should work fine.
routes.MapRoute(
name: "Default",
url: "home/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
In this scenario action will ALWAYS be Index...
Try this to catch every possible route:
routes.MapRoute(
"Default",
"home/{*url}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } }
);
I am beginner in asp.net mvc.
I have 2 Controllers:
HomeController actions: index,about
url that i need:
index action url: mydomain
about action url: mydomain/about
OtherController actions: index
index action url: mydomain/other
MyCode That not works:
routes.MapRoute(
"Other",
"{controller}/{action}/{id}",
new { controller = "Other", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Thanks
For your Home Controller, to hit
index action url: mydomain
you need
routes.MapRoute(
name: "Home",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
to hit
about action url: mydomain/about
you need
routes.MapRoute(
name: "Home",
url: "about",
defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
);
For your Other Controller, to hit
index action url: mydomain/other
you need
routes.MapRoute(
name: "Other",
url: "other",
defaults: new { controller = "Other", action = "Index", id = UrlParameter.Optional }
);
Note that, in all cases, the "name" parameter doesn't really matter.
I think you missed {controller} in your Default routing spec.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);