Show "ID" word in url in MVC - asp.net-mvc

Edit
In a href, I've pass id as parameter. so when I redirected to action I have following url
http://localhost:17888/Receptionist/UpdateCase/12
But I want id word visible in url.
http://localhost:17888/Receptionist/UpdateCase?id=12
RouteConfig
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If I change parameter name rather than id, it is working. only id is not visible in url
UpdateCase Action:
[Authorize]
[HttpGet]
public ActionResult UpdateCase(Int64 id)
{
}

One of possible solution is change your route config to
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{param}",
defaults: new { controller = "Home", action = "Index", param = UrlParameter.Optional }
);
In that case you won't have a parameter with name id in the route table, so Url.Action can't use it and as a result you wil get expected url.
But be careful and check others actions

Question marks are added when the parameter is not specified in the URL, so removing the {id} from your route will force the question mark to be added in the URL.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Related

MVC RegisterRoutes MapRoute

routes.MapRoute(
name: "Home",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Process",
url: "Process/{action}/{id}",
defaults: new { controller = "Process", action = "", id = UrlParameter.Optional }
);
1. Could you please help me in understanding why I get HTTP 404 error when I hit http://localhost:7841/Process
However, I am able to see my page when I hit
http://localhost:7841/Process/list
Also, if i hardcode controller( url: "Home/{action}/{id}") in both the routes URLs(see below) why I get “HTTP Error 403.14 - Forbidden” error.
routes.MapRoute(
name: "Home",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Process",
url: "Process/{action}/{id}",
defaults: new { controller = "Process", action = "", id = UrlParameter.Optional }
);
Kindly help me in understanding Routes.
Because when you request yourBaseUrl/Process/, It matches the route pattern {controller}/{action}/{id} which is the url pattern for your first route defined(the one called Home). So it will try to send the request to the action method and since you do not have the action method segment in the request url, it will try to use the default one defined in that route registration, which is Index. You are getting a 404 because you do not have an Index action method inside your ProcessController. If you add an Index() action method to your ProcessController, It will execute that and return the result from that.
Ideally, you should define all your specific route definition before the generic route definition. If you want /Process to return the response returned by the List method, set that as the default action in the route registration.
routes.MapRoute(
name: "Process",
url: "Process/{action}/{id}",
defaults: new { controller = "Process", action = "List", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Another option is to use the default generic route registration as it is in the RouteConfig and use attribute routing to make List method to be handled by /Process/ request.
public class ProcessController : Controller
{
[System.Web.Mvc.Route("Process")]
public ActionResult List()
{
return Content("process list action method :)");
}
}

MVC Routing Issue when trying www.example.com/id

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.

Map route in MVC to match a dot in URL

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/

URL not getting read correctly by MVC 4 routing

Hi I have a URL as follows in my MVC 4 application
http://localhost/ABC/Home/DeleteApp/3000
and I have configured RouteConfig.cs as follows
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It goes to the Correct DeleteApp Controller but with the ID value Null. It shows the correct URL when I enable firebug.
What could possibly go wrong?
it debugs and come to here but the id field as null;
public void DeleteApp(string id)
{
try
{
// delete logic
}
catch (Exception e)
{
//
}
}
Possibles causes :
-> Check your "id" variable name (must be exactly the same name in the RouteConfig and in the controller action, it is case sensitive)
-> You have other routes mapped before this one that match the pattern
If you have a route mapped as {controller}/{action}/{something} and another route mapped as {controller}/{action}/{something_else}, when you call the url "Home/DeleteApp/3000", it can't tell if 3000 is "something" or "something_else" so it will take the first match.
To make this working, you have to use "Route Constraints"
http://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC
You need to do it like so:
routes.MapRoute(
name: "ABC",
url: "ABC/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If you want to have url in this format http://localhost/ABC then you will have to modify the route as below by adding ABC to {controller}/{action}/{id} expression.
routes.MapRoute(
name: "Default",
url: "ABC/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

MVC: Multiple Routes for 1 Controller

I want users to be able to access the "/Linecard" page of my ASP.Net MVC site using "/Linecard" or "/Manufacturers" as the URL... so same controller, 2 different possible URLs.
I tried adding the following:
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
Adding this after the "Default" route doesn't work at all and I get a 404 error when I go to "/Manufacturers". Putting it BEFORE "Default" works, but then only "/Manufacturers" shows up in the URL when I click menu links since it is the first match. I would like "/Linecard" to always show as the URL.
Any pointers? Is there a certain constraint I can use to accomplish this? Thanks!
I had the same problem when we moved to extension-less URLs. We needed to continue to support one route with extensions. I got around it by having my default route apply to everything except the old URL, then after that mapping one specifically for the exception
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
// if controller specified does not match 'manufacturers' (case insensitive)
new { controller = "^((?i)(?!manufacturers).)*$" },
new string[] { "Namespace.Of.Controllers" }
);
routes.MapRoute(
"Manufacturers", // Route name
"Manufacturers/{action}/{id}", // URL with parameters
new { controller = "Linecard", action = "Index", id = UrlParameter.Optional },
new string[] { "Namespace.Of.Controllers" }
);
You could also set an order when mapping your routes with the defaults at the end like so
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Resources