My project on mvc 4. there is search bar in my website. but when i searc in search bar the url is so long for eg. www.test.com/test/test?testname=abc
i want to show shot Url like example = wwww.test.com/search?q=abc
how to change this Url
routes.MapRoute(
name: "Product",
url: "search?q=/{productName}",
defaults: new { controller = "Products", action = "SearchResult" }
);
this is not work for me. please help me
Try following code :
routes.MapRoute(
name: "Product",
url: "search/{action}",
defaults: new { controller = "Products", action = "SearchResult" }
);
Do not include query string in the route. Asp.net MVC automatically maps the query string parameters to the parameters in action method in your controller.
Related
Currently my URL looks like this:
http://localhost:9737/ProfileEdit?writerId=4
But I want it to be something like this:
http://localhost:9737/ProfileEdit/4
This is my Action signature:
public ActionResult ProfileEdit(long writerId)
I tried adding new route in RouteConfig file like this:
routes.MapRoute(
name: "ProfileEdit",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "ProfileEdit", writerId = UrlParameter.Optional }
);
but no luck.. so how do I change the URL format?
The route defines the parameter as id:
url: "{controller}/{action}/{id}"
But you're using writerId:
http://localhost:9737/ProfileEdit?writerId=4
Change the route to match:
url: "{controller}/{action}/{writerId}"
Note that this will facilitate this change on every URL which uses that route.
You can add a Route attribute:
[Route("ProfileEdit/{writerId}")]
public ActionResult ProfileEdit(long writerId)
You have two ways of doing this:
Change your route values to match the name of the parameter in your action url: "{controller}/{action}/{writerId}".
Do the opposite and change the parameter name in your action to match the route values public ActionResult ProfileEdit(long id)
Edit
If you want the same for multiple parameters you need to specify them in the route as well:
routes.MapRoute(
"ProfileEdit",
"{controller}/{action}/{writerId}/{otherParameter}",
new { controller = "Home", action = "ProfileEdit", writerId = "", otherParameter = "" });
routes.MapRoute(
name: "ProfileEdit",
url: "ProfileEdit/{id}",
defaults: new { controller = "Home", action = "ProfileEdit"}
);
I newbie on MVC5 so I got many troubles which one is
Instead of typing Home/Index/ID, I type /ID it still understand. How can I do that?
In your routes config file use the following route:
routes.MapRoute(
name: "Default",
url: "{id}",
defaults: new { controller = "Home", action = "Index" }
);
Now lets say your website name is localhost:5050 when you will hit localhost:5050/abc it will open Index action of Home controller with id = abc
I have an app that is just one controller and one action, but I want to pass two values into that action. The end result that I'm looking for is a url that looks like this http://www.example.com/parameter1/parameter2
So I was thinking that the routing would look like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{name}",
defaults: new { controller = "Home", action = "Index"}
);
and the controller would look like this
public class HomeController : Controller
{
public ActionResult Index(string id, string name)
{
return View();
}
}
But I'm clearly wrong as it doesn't work. Does anyone know if it's possible under the index action?
Just to clarify, I want 2 parameters in the default action. I'm aware it's possible by having something like http://www.example.com/books/parameter1/parameter2/ but I specifically want http://www.example.com/parameter1/parameter2/
To totally omit the controller and action placeholders in the route you can just remove them.
(Do not remove it from your default route, better create a new one and place it about the default one)
routes.MapRoute(
name: "Default",
url: "{id}/{name}",
defaults: new { controller = "Home", action = "Index"}
);
This route will only work with Index action from HomeController but not with others.
If id is optional, what your URL would look like when it's not entered, but name is?
/Home/Index//name
That's obviously invalid.
Consider using the values in the query string instead of part of the URL.
I used this and this to solve the problem.
Need two Routes and make sure these routes come above the default MVC route:
routes.MapRoute(
name: "with-name",
url: "Home/{action}/{id}/{name}",
defaults: new { controller = "Home", action = "Index"}
//No Optional
);
routes.MapRoute(
name: "without-name",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index"}
);
I have some files under the views of the area "admin", and i want to rewrite the url of some action like "agence" and "agences" without affecting "login", so how can i do it ?
This is a link to a print screen of my project.
http://hpics.li/764e0ea
If I understand your question correctly, you can solve this issue by creating a new route in your "RouteConfig" file (App_Start -> RouteConfig).
The default route for MVC is:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
You can use this as a template for creating a new route as well. Simply place the new route above the "Default" route. Something like the following:
routes.MapRoute(
name: "Any distinct name",
url: "{controllerName (judging by your question, probably }/{variable1}/{variable2}/{etc... if needed}",
defaults: new
{
variable1 = UrlParameter.Optional,
variable2 = UrlParameter.Optional
}
);
You can then pass your chosen variables as RouteValues in ActionLinks.
#Html.ActionLink("LinkText", "Action", new { variable1 = whatever, variable2 = whateverElse})
I am using .Net Framework 4.5 and MVC4 with Entity Framework.
I currently have the following code in my RouteConfig.cs:
routes.MapRoute(
name: "Jobs",
url: "job_openings.aspx",
defaults: new { controller = "AboutUs", action = "Index", id = UrlParameter.Optional }
);
But we now have a page anchor in the About Us page. When a user types in /job_openings.aspx, I want them to be taken right down to that anchor. Does anyone know how to do that?
I've tried doing a MapPageRoute, but that doesn't seem to work:
routes.MapPageRoute("Jobs", "job_opening.aspx", "~/AboutUs#News");
This is the URL I am trying to get to: ~/AboutUs#News from this one: job_opening.aspx
Is this possible?
Sooo thanks to this amazing post here: ASP.NET MVC Redirect to action with anchor
I was actually able to get this to work...BUT I had to do some...finagling.
In the Route config I added the id of 1 so I could handle it in the controller:
routes.MapRoute(
name: "Jobs",
url: "job_openings.aspx",
defaults: new { controller = "AboutUs", action = "Index", id = 1 }
);
This is what I did in my controller to handle this request (Using what that one page provided):
public ActionResult Index(int id = 0)
{
if (id == 1)
{
return Redirect(Url.RouteUrl(new { controller = "AboutUs", action = "Index" }) + "#News");
}
return View();
}
Now here is the tricky part I had to bang my head on for a while. For some reason on the redirect, it literally went to the route config and took the first item that had controller = "AboutUs", action = "Index" and went to it. So I was in an infinite loop.
So I added this ON TOP of the "Jobs" route:
routes.MapRoute(
name: "AboutUs",
url: "AboutUs",
defaults: new { controller = "AboutUs", action = "Index", id = UrlParameter.Optional }
);
Once I did that, it actually worked!