how to route url having QueryString to specific ActionResult of Controller - c#

I am working on ASP.NET MVC 3 in C# and I want to route the URL consisting of QueryString to desired controller and action Method.
URL will be like localhost:44578/HVAC/?pos=installer I want to route this. I don't know how to do that.
I am new to MVC.
default route in RouteConfig is
routes.MapRoute(
name: "Default",
url: "{siteName}/{controller}/{action}/{id}",
defaults: new { controller = "SeoTree",
action = "Index", id = UrlParameter.Optional }
);
I want to create new Map route which routes the url to Contoller = "SeoTree" ,action ="PositionInAll"

just map the route as you wanted like:
routes.MapRoute("Custom",
"{controller}/{action}/{pos}",
new { controller = "seoTree", action = "PositionInAll"},
new { pos = #"\d+" }
);

Related

c# Route config for specific URL only

I have this URL
http://localhost:51095/Person/Walk/10
Using RouteConfig how can I remove the controller part without affecting other URL's.
So this will become
http://localhost:51095/Walk/10
This is the order also, they are affecting other URL's
routes.MapRoute(
"Walk", // Route name
"{action}/{distance}", // URL with parameters
new { controller = "Person", action = "Walk" } // Parameter defaults,
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
But this one also affected other Routes
You'll have to be more specific:
routes.MapRoute("Walk",
"Walk/{distance}",
new { controller = "Person", action = "Walk" });

MVC ActionLink appending route as querystring rather than route values

I have an MVC Action Link:
#Html.ActionLink("Update Information", "Index", "Performance",
new { performanceid = item.PerformanceId }, null)
This action link's href looks like this: /Performance/Index?performanceid=100
In my RouteConfig.cs I have the following routes in the following order:
routes.MapRoute(
"ShowPerformanceOptions",
"Performance/{performanceid}/Index",
new { controller = "Peformance", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I do not want a querystring added to the end of the URL, I would instead like the URL to look like this: /Performance/360/Index
I have been through a variety of different options including adding the route parameters and optional url parameters and changing the way I write my ActionLink. Nothing seems to work.
Any ideas anyone?
To generate URL based on route name, use Html.RouteLink() method
#Html.RouteLink("Update Information", "ShowPerformanceOptions", new { performanceid = item.PerformanceId })
A good read What's the difference between RouteLink and ActionLink in ASP.NET MVC?
As #Satpal pointed out, the ActionLink wasn't working because of the typo in the route itself:
routes.MapRoute(
"ShowPerformanceOptions",
"Performance/{performanceid}/Index",
new { controller = "**Peformance**", action = "Index" }
);
routes.MapRoute(
"ShowPerformanceOptions",
"Performance/{performanceid}/Index",
new { controller = "**Performance**", action = "Index" }
);

How do I map the root directory to a controller in c# using web api?

I have a c# web api (.net 4) web application. I have several controllers. I'd like to map a new controller to the root directory for the web app.
So, I have http://localhost/listproducts
I'd like the url to be http://localhost
But I don't know how to tell the controller to use the root directory. It seems to be configured based on the name.
The solution I went with is:
config.Routes.MapHttpRoute(
name: "ListProductsApi",
routeTemplate: "",
defaults: new { controller = "ListProducts" } // Parameter defaults
);
The trick is the defaults: new { controller = "ListProducts" } line. (I need a POST action so I just left action out. Apparently when you want the root route "/" you need to explicitly name the controller.
You simply need to change the default route you already have. It should currently look something like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
Change controller = "Home" to controller = "listproducts".
It should be fairly easy, just change a default route in your global.asax.cs and instead of
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
change it to
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "ListProducts", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Set default action (instead of index) for controller in ASP.NET MVC 3

I have a controller called Dashboard with 3 actions: Summary, Details, and Status, none of which take an ID or any other parameters. I want the URL /Dashboard to route to the Summary action of the Dashboard controller, as /Dashboard/Summary does, but I can't figure out the correct way to add the route. In Global.asax.cs, I have the following:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
routes.MapRoute(
"/Dashboard",
"Dashboard",
new { controller = "Dashboard", action = "Summary" }
);
For the second part, I've also tried:
routes.MapRoute(
"/Dashboard",
"{controller}",
new { controller = "Dashboard", action = "Summary" }
);
and
routes.MapRoute(
"/Dashboard",
"{controller}",
new { action = "Summary" }
);
but I always get a 404 when trying to access /Dashboard. I'm pretty sure I'm missing something about the format for the parameters to MapRoute, but I don't know what it is...
Move your Dashboard route in front of the Default route:
routes.MapRoute(
"Dashboard",
"Dashboard/{action}",
new { controller = "Dashboard", action = "Summary" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
The order of routes changes everything. Also, notice the changes I made to the Dashboard route. The first parameter is the name of the route. Second is the URL, which match URLs that start with Dashboard, and allows for other actions in your Dashboard controller. As you can see, it will default to the Summary action.
You need to declare the "Default" catch-all route last.
This set default Action for any Controller asp.net:
routes.MapRoute("Dashboard", "{controller}/{action}",
defaults: new { controller = "Dashboard", action = "Summary" });

MVC Razor routing suggestion

I have a web site with something like this:
http://website/Controller/Action/Id
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Controller", action = "Action", id = UrlParameter.Optional } // Parameter defaults
);
Is there any way to route to the same action if the url changes to be
http://website/Action/Id
I was trying to change global.asax but nothing is working.
Based on how I'm reading your question, you're saying that you may only ever have one controller and want to be able to route all actions to that single controller?
If I'm reading it correctly, you should be able to do something similar to this:
routes.MapRoute(
"Default", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Controller", action = "Action", id = UrlParameter.Optional } // Parameter defaults
);

Categories