MVC ActionLink appending route as querystring rather than route values - c#

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" }
);

Related

RegisterRoutes in Global.asax

I dont understand the RegisterRoutes perfectly. Lets assume the browser's current URL is
//Home/ListCompanies/{filter}
http://localhost:21047/Home/ListProducers/Yerli
we came to the address above with the link below
#Html.ActionLink("Yerli Markalar", "ListProducers/Yerli", "Home")
And I have a link on the current page like below
#Html.ActionLink("Bayiler", "ListCompanies", "Home")
http://localhost:21047/Home/ListCompanies/Yerli
But when I hover on the last link, it displays "Yerli" filter as well which I dont give it in the link
Why does the filter "Yerli" come here? It should seem like below
http://localhost:21047/Home/ListCompanies/
What am I doing wrong?
here are my Routes
routes.MapRoute(
null,
"Home/ListProducers/{filter}", // Route name
new { controller = "Home", action = "ListProducers", filter = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
null,
"Home/ListCompanies/{filter}", // Route name
new { controller = "Home", action = "ListCompanies", filter = UrlParameter.Optional } // Parameter defaults
);
Firstly, you do not use
#Html.ActionLink("Yerli Markalar", "ListProducers/Yerli", "Home")
If "Yerli" is the parameter to be passed to
public ActionResult ListProducers(string filter)
then it needs to be
#Html.ActionLink("Yerli Markalar", "ListProducers", "Home", new { filter = "Yerli" }, null)
which will generate ..../Home/ListCompanies/Yerli
Your problem is that you have generated the action parameter as ListCompanies/Yerli whereas it should be ListCompanies
Note you should also name your routes
routes.MapRoute(
"ListProducers", // name it!
"Home/ListProducers/{filter}", // Route name
new { controller = "Home", action = "ListProducers", filter = UrlParameter.Optional } // Parameter defaults
);
However, those routes seem unnecessary and you could just delete them and let the default route handle it
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
and just change the method to
public ActionResult ListProducers(string id)
and use
#Html.ActionLink("Yerli Markalar", "ListProducers", "Home", new { id = "Yerli" }, null)
which will produce exactly the same result

how to route url having QueryString to specific ActionResult of Controller

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+" }
);

Custom URL in c# MVC 4

I'm trying to add custom URL to my application. For example, I have a report section and I want all the Controller in that section to start with "Report". Currently, my URL look like :
localhost:12345/MyReport
but I want it to look like
localhost:12345/Report/MyReport
MyReport is the name of my controller.
I tried to change the following code in Global.asax
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
By
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Report",
"Report/{controller}/{action}/{id}",
new { controller = "MyReport", action = "Index", id = UrlParameter.Optional }
);
But I get a page not found error when I try to access localhost:12345/Report/MyReport... Any clue?
As crazy as this is at first, order matters. Routing will find the first route that matches, and serves it. Try putting the the default one after your new custom one.
Because of that, you're going to the controller of Report, and attempting to find the action of MyReport. Which is indeed not found.

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
);

MVC 3 How to use MapRoute

Could someone show me how to use the MapRoute method? I have tried creating my own routes, but it's not working. What i want to accomplish is a route that routes "http://servername/home/default.aspx" into controller "Home" and action "Default". Also, would it be possible to say that if the user is browsing the default.aspx "file", it would actually point to the "Index" action?
I have tried reading the MSDN references and googling, but it didn't make me any wiser.
Probably too late to help the developer who raised the question but may help someone else. New to MVC but what I found is the map routes seem to be processed in the order they are added. I had a similar problem, my specific route was not working until I started adding the default route as the last route.
If the default map route is added before your custom one and your custom URL matches the structure defined by the default map route you will never reach your custom route.
The route you want to configure the first part of your question is:
routes.MapRoute(
"",
"home/default.aspx",
new { controller = "Home", action = "Default" }
);
Assuming you wish to 'browse' default.aspx with some sort of parameter you can do something like:
routes.MapRoute(
"",
"home/default.aspx/{param}",
new { controller = "Home", action = "Default", param = UrlParameter.Optional }
);
And you would then need to create your Default action to accept string param.
You also have to make sure the parameter name is the same as the action's parameter name.
Example:
routes.MapRoute(
name: "MyName",
url: "{controller}/{action}/{myParam}",
defaults: new { controller = "MyController", action = "MyAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
MyController:
public ActionResult MyAction(string myParam = "")
{
}

Categories