MVC Preview 4 - No route in the route table matches the supplied values - asp.net-mvc

I have a route that I am calling through a RedirectToRoute like this:
return this.RedirectToRoute("Super-SuperRoute", new { year = selectedYear });
I have also tried:
return this.RedirectToRoute("Super-SuperRoute", new { controller = "Super", action = "SuperRoute", id = "RouteTopic", year = selectedYear });
The route in the global.asax is like this:
routes.MapRoute(
"Super-SuperRoute", // Route name
"Super.mvc/SuperRoute/{year}", // URL with parameters
new { controller = "Super", action = "SuperRoute", id = "RouteTopic" } // Parameter defaults
);
So why do I get the error: "No route in the route table matches the supplied values."?
I saw that the type of selectedYear was var. When I tried to convert to int with int.Parse I realised that selectedYear was actually null, which would explain the problems. I guess next time I'll pay more attention to the values of the variables at a breakpoint :)

What type is selectedYear? A DateTime? If so then you might need to convert to a string.

Related

sending string from view to controller in asp.net

I'm trying to get data from view in string
Here's the code in View
And in controller, I'm getting this customer name like this:
public ActionResult ViewCustomerDetails(string c_name) {
List<Sale> customerList = new List<Sale>();
customerList = db.Sales.Where(x => x.sale_customer == c_name).ToList();
double total_cash_recieved = 0;
double total_amount = 0;
foreach (var customer in customerList) {
total_cash_recieved = total_cash_recieved + (double)customer.cash_recieved;
total_amount = total_amount = (double)customer.sale_amount;
}
double remaining_balance = total_amount - total_cash_recieved;
ViewBag.TotalAmount = total_amount;
ViewBag.TotalRecieved = total_cash_recieved;
ViewBag.TotalRemaining = remaining_balance;
return View(customerList);
}
But the problem is, in c_name variable, I'm getting null.
Anyone know how to correct it or solution?
Since your parameter name is c_name, you should include that in your querystring as Burak mentioned in his answer.
If you prefer, you can render the link using Html.ActionLink helper method.
Html.ActionLink("View","ViewCustomerDetails","Admin",new { c_name=customer.name},null)
Or if you prefer to keep the existing url you have, you can update your ViewCustomerDetails method's parameter name to Id so that with the default route definition, your unnamed parameter value will be mapped to Id parameter.
public ActionResult ViewCustomerDetails(string id) {
var c_name=id;
// your existing code
}
It is always a good idea to pass a unique Id ( Customer ID etc..) instead of passing a name to show the details because I know more than one scott in the world.
You should send it like this:
And make sure that #customer.name is not null before going to the server side.
or you can set new route to RouteConfig.cs
routes.MapRoute(
name: "Default2",
url: "Admin/ViewCustomerDetails/{c_name}",
defaults: new { controller = "Admin", action = "ViewCustomerDetails", c_name= UrlParameter.Optional }
);
You didn't pass the parameter to the controller.
You can always simply pass parameters as part of query string as long as action method on controller is expecting them by exactly the the same name in the signature.

Redirect To Action method not mapping correctly

I'm calling RedirectToAction but it isn't working properly.
I want the resulting URL to look like this:
https://localhost:44301/ManageSpaces/123/overview
but it looks like this and is missing the action portion of the URL:
https://localhost:44301/ManageSpaces/123
Here is my RedirectToAction call.
return RedirectToAction("overview", new RouteValueDictionary(
new {controller = "ManageSpaces", action = "overview", id = 123}));
Here is what my route looks like in RouteConfig:
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces", action = "overview"},
new { id = #"\d+" } //The regular expression \d+ matches one or more integers
);
Maybe it is taking the default route. Rename, remove, or comment out the default route to see if that has any effect.
You have made your action route value optional by providing a default value. Optional values are ignored when resolving the URL.
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces", action = "overview"},
new { id = #"\d+" } //The regular expression \d+ matches one or more integers
);
If you want to include the action in the URL, you have to make it a required argument.
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces"},
new { id = #"\d+" } //The regular expression \d+ matches one or more integers
);

ASP.NET MVC - Routes and UrlHelper

I have the following route
routes.MapRoute(
"GigDayListings", // Route name
"gig/list/{year}/{month}/{day}", // URL with parameters
new { controller = "Gig", action = "List" },
new
{
year = #"^[0-9]+$",
month = #"^[0-9]+$",
day = #"^[0-9]+$"
} // Parameter defaults
);
When I visit the URL
gig/list/2009/01/01
This route matches perfectly and my action is called.
Inside my view I have a helper which does the following:
var urlHelper = new UrlHelper(ViewContext);
string url = urlHelper.RouteUrl(ViewContext.RouteData.Values);
The string generated is:
http://localhost:3539/gig/list?year=2005&month=01&day=01
Why is it not
http://localhost:3539/gig/list/2005/01/01
What am I doing wrong?
I think your problem is that you didn't specify the route name in your call. Try to use
UrlHelper.RouteUrl(**"GigDayListings"**, ViewContext.RouteData.Values);
overload with route name.
Cheers!
Have you checked that when you supply gig/list/2008/01/01 that it is actually using the GigDayListings route? Maybe it's using a different one

ASP.NET MVC - Mapping more than one query string parameter to a pretty url

I am a bit stuck on the design of my seo friendly urls for mvc....Take for example the following url:
http://myapp/venues/resturants.aspx?location=central&orderBy=top-rated
With my mvc app i have mapped it as follows:
http://myapp/venues/list/resturants/central/top-rated
{controller}/{action}/{category}/{location}/{order}
Now the only problem is that location and order are optional...so it should be possible to submit a request like: http://myapp/venues/list/resturants/top-rated . This proves to be a problem when the request hits the controller action, the location parameter has picked up "top-rated", naturally.
Any suggestions? I' am considering using explicit querystrings to handle more than one parameter but this is really my last option as i dont want to sacrifice SEO too much.
Has anyone eles run into such dilemmas? And how did you handle it?
Thanks in advance!
Click on your profile link and look at the URLs for Stats, Recent, Response, etc.
Examples:
https://stackoverflow.com/users/52065?sort=recent#sort-top
https://stackoverflow.com/users/52065?sort=stats#sort-top
with no sort it defaults to stats
https://stackoverflow.com/users/52065
Optional paramters should be query parameters
Assuming that the allowed values for location and order are unique (i.e. when they come in, you can tell them apart, or else if they only supply one, how are you going to know if it's a location or an order?), then you could just take two parameters and work out what they are in the controller.
Route: {controller}/{action}/{param1}/{param2}
Controller action:
public ActionResult MyAction(string param1, string param2)
{
string location;
string order;
if (!ParseLocation(param1, out location))
{ ParseLocation(param2, out location); }
// ...
}
Not particularly elegant, but does let you have the URLs you want.
You will always have this issue if you have multiple optional parameters. Either make one or both of them non-optional (and positioned earlier in the query string than the optional one) or use the querystring parameter notation.
ok guys just posting a solution i've been playing with so far.
I have set up my routes using constraints as follows:
routes.MapRoute(
"VenuesList",
"venues/list/{category}/{location}/{orderBy}",
new { controller = "venues", action = "list", category = "", location = "", orderBy = "" },
new { location = "central|east|west|south", orderBy = "top-rated|price" }
);
routes.MapRoute(
"VenuesListByLocation",
"venues/list/{category}/{location}",
new { controller = "venues", action = "list", category = "", location = "" },
new { location = "central|east|west|south" }
);
routes.MapRoute(
"VenuesListByOrder",
"venues/list/{category}/{orderBy}",
new { controller = "venues", action = "list", category = "", orderBy = "" },
new { orderBy = "top-rated|price" }
);
routes.MapRoute(
"VenuesListDefault",
"venues/list/{category}",
new { controller = "venues", action = "list", category = "" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The idea is that if the validation fails it will go to the next route in the list...eventually hitting the default.
Needs some more testing but has worked well so far...
Why don't you create a property in the page for each possible querystring parameter?
This way you can handle it any way you choose with just a few lines of code...

Creating urls with asp.net MVC and RouteUrl

I would like to get the current URL and append an additional parameter to the url (for example ?id=1)
I have defined a route:
routes.MapRoute(
"GigDayListings", // Route name
"gig/list/{year}/{month}/{day}", // URL with parameters
new { controller = "Gig", action = "List" } // Parameter defaults
);
In my view I have a helper that executes the following code:
// Add page index
_helper.ViewContext.RouteData.Values["id"] = 1;
// Return link
var urlHelper = new UrlHelper(_helper.ViewContext);
return urlHelper.RouteUrl( _helper.ViewContext.RouteData.Values);
However this doesnt work.
If my original URL was :
gig/list/2008/11/01
I get
gig/list/?year=2008&month=11&day=01&id=1
I would like the url to be:
controller/action/2008/11/01?id=1
What am I doing wrong?
The order of the rules makes sence. Try to insert this rule as first.
Also dont forget to define constraints if needed - it will results in better rule matching:
routes.MapRoute(
"GigDayListings", // Route name
"gig/list/{year}/{month}/{day}", // URL with parameters
new { controller = "Gig", action = "List" }, // Parameter defaults
new
{
year = #"^[0-9]+$",
month = #"^[0-9]+$",
day = #"^[0-9]+$"
} // Constraints
);

Resources