I have a search box on my page (.cshtml) with 3 drop down boxes (Country, State, City). When I click on Search button, the URL needs to be changed to like below.
http://localhost:1234/OrderHistory/Orders (default)
When I click on search with values in dropdowns, the url is to be changed to
http://localhost:1234/OrderHistory/Orders/dropbox1val/dropbox2val/dropbox3val
How to rewriting in asp.net mvc 4?
You need to refine your URL Scheme in the RegisterRoutes(RouteCollection routes)method. I think somethink like the following might give you a clue.
routes.MapRoute("RouteName", "Dropdown1{variableNameHoldingTheValue}/Dropdown2{variableNameHoldingTheValue}/DropDown3{variableNameHoldingTheValue}",
new { controller = "ControllerName", action = "ActionName" },
// You might add consrtaints here
);
EDIT:
if you want this URL to be your default
http://localhost:1234/OrderHistory/Orders (default)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "OrderHistory", action = "Orders", id = UrlParameter.Optional }
);
Related
I have three areas: tpl1 - tpl2 - tpl3
And when I start the application I choose which area to use:
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" },
new[] { "LojaOnline.Controllers" }
).DataTokens.Add("area", "tpl1");
(The "tpl1" in DataTokens.Add is dynamic.)
The Url is: http://localhost/tpl1/Home/Index
I need to hide the name of the area in the URL. Something like that:
The Url is: http://localhost/Home/Index
I have this in tpl1AreaRegistration.cs
context.MapRoute(
"tpl1_default",
"tpl1/{controller}/{action}/{id}",
new {controller="Home", action = "Index", id = UrlParameter.Optional }
);
But if I remove the tpl1, the application doesn't know which controller use.
context.MapRoute(
"tpl1_default",
"{controller}/{action}/{id}",
new {controller="Home", action = "Index", id = UrlParameter.Optional }
);
As far as I know in IIS its not possible.
When you give the URL to be http://localhost/Home/Index.
First it will connect to your local IIS and inside which it will search for the Application Name as "Home" and inside it will search for the scripting page or webforms as per the Web application which you choose. But actually as per your URL you have specified your application to be tpl1.
So you cannot use http://localhost/Home/Index instead of http://localhost/tpl1/Home/Index
But yes you can hide the Index from the URL at the end like http://localhost/tpl1/Home.
Using Route file you can only control the URL's inside the application.
This is related my question which i asked in this link correct me on url routing in mvc
Now i came with another problem, so i thought i will ask it as new question.
Now i have following routes in my global.asax file
routes.MapRoute(
"Custom", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
and
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
Now what happens is when i run my solution the URL i am getting is http://localhost:65423/Login this is what i need for my Login Page that is OK. But when i login in as user i am getting "The resource cannot be found" error.
when i checked it i can see that my URL is now changed to http://localhost:65423/Admin/Dashboard
So i think this causing the issue. So this looks the problem related to my global.asax routing.
Can anyone help me to find out what i did wrong.
You have 2 routes with completely optional segments. The issue is that there is no way for the routing framework to differentiate between them.
The only way you can make it work with your existing routes is to specify them explicitly by name (such as when using #Html.RouteLink or #Html.RouteUrl).
#Html.RouteLink("Custom Link 1", "Custom", new { action = "BigClientLogin" })
#Html.RouteLink("Custom Link 2", "Custom", new { action = "Action2" })
#Html.RouteLink("Home Page", "Default", new { controller = "Home", action = "Index" })
#Html.RouteLink("About", "Default", new { controller = "Home", action = "About" })
Doing it that way will function, but is not normal. Typically, there is only one route configured with all defaults for the controller, action, and id and the rest have some explicitly declared segments and/or constraints (segments being preferable).
routes.MapRoute(
"Custom", // Route name
"Custom/{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
The first route will now only match when the URL starts with /Custom/. If it does not start with custom, it will match the default route.
The trick is to ensure that the routes are listed in the right order and that they only match the URL in specific cases, letting them pass on to the next route in the list if the case is not correct.
It happens because of 2 things.
1st the sequence of Your route
2nd your defaults
So if you end up on 'admin/dashboard' that's not a default, guess you've just put it in your studio to start there?
To get to http://localhost:65423/Login you'll need an action on Your AuthenticationController called 'Login' but it looks like the one You have configured is 'BicClientLogin' so You won't be taken to login unless you specify it, and at that time it has to exist.
To Help You further we need to know what controllers You have and what actions there exists, plus if security is part of your solution and if in case, what is is set to use.
Ok, so i'm new to asp.net mvc and i'm trying to make a web application photo gallery. I've posted once on here about this issue i am having of trying to generate thumbnails on-the-fly on the page instead of the actual full-size images. Basically, the functionality i am looking for is to be able to have thumbnails on the page and then be able to click the images to see the full-size version. I am pulling the images and images info from an XML file. So, i did this so i could display them dynamically and so it would be easier to make changes later. Later, i am going to add functionality to upload new images to specific galleries (when i figure out how to do that as well). I am providing a link to download the project i am working on so you can see the code. I would appreciate any help with this! Thanks!
URL to project: http://www.diminished4th.com/TestArtist.zip
Ryan
In your global.asax.cs file, you have defined the Default route before your Thumbs route so the Galleries part of the url is mapped to a non-existent Galleries controller instead of the Gallery controller (as specified in your second route):
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Thumbs", // Route name
"Galleries/getThumb/{image}", // URL with parameters
new { controller = "Gallery", action = "getThumb", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "TestArtist.Controllers" }
);
Simply define the Thumbs route before the Default route and you should be all good:
routes.MapRoute(
"Thumbs", // Route name
"Galleries/getThumb/{image}", // URL with parameters
new { controller = "Gallery", action = "getThumb", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "TestArtist.Controllers" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I am learning MVC and I need to understand why it doesn't work the way it should.
Here is my routing :
public static void RegisterRoutes(RouteCollection routes)
{
// Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
// automatic support on IIS6 and IIS7 classic mode
//http://localhost/store/category/subcategory/product
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Category", // Route name
"store/{category}/{subcategory}", // URL with parameters
new
{
controller = "Catalog",
action = "Index",
category = "Featured Items",
subcategory = "All Items"
}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
new { controller = #"[^\.]*" } // Parameter constraints
);
}
The way I understand routing I should see the following url when I start the web app :
http:/localhost/store/
What I get is the second route....
Furthermore if I change the second route to "home/{action}/{id} it doesn't catch any routes.
Could you help me understand this please..Thanks
Routes do not specify default URL; the default URL is handled by your app. Routing specifies that when it sees http://localhost/store/bikes/mountain, it will use the catalog controller. But that doesn't specify the default URL; you have to enter that in the project properties.
I would recommend not changing the second one because unless you are creating groupings for all of your controllers, it's best to have the default as it is so you can catch all URL's. Your change to the second one would require the url to be:
http://localhost/home/home/index to match the HomeController's index action, whereas the default setup catches http://localhost/home/index...
Does that make sense?
Try this: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
How do I generate friendly URLs within the ASP.NET MVC Framework? For example, we've got a URL that looks like this:
http://site/catalogue/BrowseByStyleLevel/1
The 1 is Id of the study level (Higher in this case) to browse, but I'l like to reformat the URL in the same way StackOverflow does it.
For example, these two URLs will take you to the same place:
https://stackoverflow.com/questions/119323/nested-for-loops-in-different-languages
https://stackoverflow.com/questions/119323/
EDIT: The friendly part of the url is referred to as a slug.
There are two steps to solve this problem. First, create a new route or change the default route to accept an additional parameter:
routes.MapRoute( "Default", // Route name
"{controller}/{action}/{id}/{ignoreThisBit}",
new { controller = "Home",
action = "Index",
id = "",
ignoreThisBit = ""} // Parameter defaults )
Now you can type whatever you want to at the end of your URI and the application will ignore it.
When you render the links, you need to add the "friendly" text:
<%= Html.ActionLink("Link text", "ActionName", "ControllerName",
new { id = 1234, ignoreThisBit="friendly-text-here" });
This is how I have implemented the slug URL on my application.
Note: The default Maproute should not be changed and also the routes are processed in the order in which they're added to the route list.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home",
action = "Index",
id = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute("Place", "{controller}/{action}/{id}/{slug}", new { controller = "Place", action = "Details", id = UrlParameter.Optional,slug="" });
you have a route on the global.asax
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = ""}
// Parameter defaults )
you can define your own route like :
controller is the cs class inside the the controllers folder.
you can define your id - with the name you choose.
the system will pass the value to your actionResult method.
you can read more about this step here : http://www.asp.net/learn/mvc/tutorial-05-cs.aspx