rewrite routes for URl - asp.net-mvc

I have the following URl:
http://localhost:12981/BaseEvent/EventOverview/12?type=Film
This is route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I want that in a browser the url looks like:
http://localhost:12981/Film/Overview/12
How can I do this?
One more example:
http://localhost:12981/BaseEvent/EventOverview/15?type=Sport
should be
http://localhost:12981/Sport/Overview/15
Thanks.

This should work:
routes.MapRoute("", "{type}/Overview/{id}", new { controller = "Events", action = "Overview");
Then you have a controller named EventsController with an action like this one
public ViewResult Overview(string type, int id)
{
//Your code
return View(model);
}

Related

Change url asp.net mvc 5

routes.MapRoute(
name: "MyRoute",
url: "{Product}/{name}-{id}",
defaults: new { controller = "Home", action = "Product", name = UrlParameter.Optional , id = UrlParameter.Optional }
);
my routemap and i want my url in product action be like = http://localhost:13804/Wares/Product/name-id
but now is like =
http://localhost:13804/Wares/Product/4?name=name
When defining a route pattern the token { and } are used to indicate a parameter of the action method. Since you do not have a parameter called Product in your action method, there is no point in having {Product} in the route template.
Since your want url like yourSiteName/Ware/Product/name-id where name and id are dynamic parameter values, you should add the static part (/Ware/Product/) to the route template.
This should work.
routes.MapRoute(
name: "MyRoute",
url: "Ware/Product/{name}-{id}",
defaults: new { controller = "Ware", action = "Product",
name = UrlParameter.Optional, id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Assuming your Product action method accepts these two params
public class WareController : Controller
{
public ActionResult Product(string name, int id)
{
return Content("received name : " + name +",id:"+ id);
}
}
You can generate the urls with the above pattern using the Html.ActionLink helper now
#Html.ActionLink("test", "Product", "Ware", new { id = 55, name = "some" }, null)
I know its late but you can use built-in Attribute Routing in MVC5. Hope it helps someone else. You don't need to use
routes.MapRoute(
name: "MyRoute",
url: "{Product}/{name}-{id}",
defaults: new { controller = "Home", action = "Product", name = UrlParameter.Optional , id = UrlParameter.Optional }
);
Instead you can use the method below.
First enable attribute routing in RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
Then in WaresController
[Route("Wares/Product/{name}/{id}")]
public ActionResult Product(string name,int id)
{
return View();
}
Then to navigate write code like this in View.cshtml file
Navigate
After following above steps your URL will look like
http://localhost:13804/Wares/Product/productname/5

Asp.net Routes with two routes

I have two route with same signature but only the parameter names are different how to fix this issue.
Following is my code
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultRoute2",
url: "{controller}/{action}/{formSubmissionId}",
defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
);
The routes are meant to accept different URL structures. Both of your routes have same structure, so the first one will always match, and the second will never be tested.
Instead of using a different route, in /Employee/Index you should just use the parameter id.
public class EmployeeController : Controller
{
public ActionResult Index(string id)
{
string formSubmissionId = id;
}
}
The URL for that action would be the same that (I believe) you wanted to achieve with the second route: Employee/Index/id
UPDATE
I've just realized. If you only need the parameter formSubmissionId for the action /Employee/Index you could do this:
// Note the order of the routes:
routes.MapRoute(
name: "DefaultRoute2",
url: "Employee/Index/{formSubmissionId}",
defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
public class EmployeeController : Controller
{
public ActionResult Index(string formSubmissionId)
{
// ...
}
}
Now i fix this issue as
routes.MapRoute(
name: "DefaultActivity",
url: "{controller}/LoadActivity/{formSubmissionId}",
defaults: new { controller = "{controller}", action = "LoadActivity", formSubmissionId = UrlParameter.Optional }
);
this is work around of my scenario.

Url.Action does not send route parameters

I want my url be like:
localhost:22504/Apps/App/{name}/{id}/{type}
So I added route values to Global.asax file:
routes.Add("AppRoute", new MyRoute("App/{name}/{id}/{type}", new { controller = "Apps", action = "App" }));
routes.Add("AppRoute2", new MyRoute("App/{id}/{type}", new { controller = "Apps", action = "App" }));
Those routes placed above Default route which looks like:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And my controllers:
public ActionResult App(string id, string name, string type)
{
//code here
}
My Url.Action:
href="#Url.Action("App", "Apps", new { id = GuidEncoder.Encode(app.AppID), name = "Read more", type = "review"})
Url.Action renders as:
href="/Apps/App/lBF8BPwVykebNe9XU1KOOg?name=21webmerce-manager1"
What am I missing?

MVC3 maproute where first value of URL is querstring for default controller and action

I want to make a route something like this:
routes.MapRoute(
"Default", // Route name
"{s}", // URL with parameters
new { controller = "Home", action = "Index", s = UrlParameter.Optional }
);
Where s is a parameter for the default controller and action.. Is this possible? i would also settle for something like:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{s}", // URL with parameters
new { controller = "Home", action = "Index", s = UrlParameter.Optional }
);
routes.MapRoute(
"qMap", // Route name
"sc/{s}", // URL with parameters
new { controller = "Home", action = "Index", s = UrlParameter.Optional }
);
But neither work.. i suspect because the first element/paramater is always expected to be a controller?
If you have only the following route definition (make sure you have removed all other route definitions from your RegisterRoutes method):
routes.MapRoute(
"Default",
"{s}",
new { controller = "Home", action = "Index", s = UrlParameter.Optional }
);
and the following controller:
public class HomeController: Controller
{
public ActionResult Index(string s)
{
...
}
}
a request of the form http://foo.com/abc would be routed to the Home controller and the Index action will be invoked and passed s=abc.

How do I set up RESTFul URLs using ASP.NET MVC routing?

I have this URL:
/controller/action/value
and this action:
public ActionResult Get(string configName,string addParams)
{
}
How do I set up my routing table to get the routing engine bind the value to the configName parameter for any action in the Config controller?
Well, first off, that is incomplete. You don't have a method name.
Secondly, this will already work with URLs of the format:
/controller/action?configName=foo&addparams=bar
Here's how to do it with pretty routes:
routes.MapRoute(
"YourMapping",
"{controller}/{action}/{configName}/{addParams}");
or
routes.MapRoute(
"YourMapping",
"{controller}/{configName}/{addParams}",
new {
controller = "YourController",
action = "YourAction"
},
new {
controller = "YourController" // Constraint
});
if you want to exclude the action from the URL.
You could add a new route above the default
routes.MapRoute(
"Config",
"config/{action}/{configName}/{addParams}",
new { controller = "Config", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Which will allow you to use the route /config/actionName/configName/addParamsValue. Your other routes should be unaffected by this.
routes.MapRoute(
"ValueMapping",
"config/{action}/{configName}/{addParams}",
new { controller = "Config", action = "Index", configName= UrlParameter.Optional, addParams = UrlParameter.Optional } // Parameter defaults);
Setting default Controller to Home, with a Default Action of Index
So the Url:
/config/get/configNameValue/AddParamValue
would match this Method:
public ActionResult Get(string configName,string addParams)
{
//Do Stuff
}

Resources