MVC4 Razor Appending ID to a url using route.config - asp.net-mvc

I just cant achieve my goal of getting an id to appear in a URL. Here is an example of what I have done so far.
Here is my BlogController:
public ActionResult BlogPost(int hiddenBlogId)
{
TempData["id"] = hiddenBlogId;
return View(_repository);
}
Here is my route.config:
routes.MapRoute(
"MyBlog", // Route name
"blog/{action}/{id}", // URL with parameters
new { controller = "Blog", action = "blogpost", id = #"0|-?[1-9]\d*" } // Parameter defaults
);
I am completely missing the point somewhere. How can I pick up the parameter which went into my method/action BlogPost and then display it in the output URL.
http://www.mydomain/controller/id
It's so that in the end I should be able to display the title for each blog. I'm using an ID just for simplicity for now. Any help at all would be greatly appreciated.

Your route definition says that the third value is called id, but you are trying to bind hiddenBlogId in the method. The two names need to match. Change the hiddenBlogId action method parameter to id, or map a new route with the {hiddenBlogId} placeholder.

Related

How do I make friendy URLs in ASP.NET MVC with all products in the root?

I'm in the process of building a web shop in ASP.NET MVC. What I would like to accomplish is a list of all the products in the root of the web shop. In other words I would like a SEO optimized friendly URL with the names of the products directly after the base URL. For example:
myexamplewebshop.com/beautiful-red-coat
myexamplewebshop.com/yellow-t-shirt
If you click on one of these links, you will see the details page of the product. I guess I need to change something in the routing code somewhere to make this work. Can anyone give me an example of how to accomplish this? Any help would be greatly appreciated.
Add a route like:
routes.MapRoute(
"SEO_Product", // Route name
"{seoterm}",
new { controller = "Product", action = "LookupBySEO" }
);
Then in your Product Controller add method:
public ActionResult LookupBySEO(string seoterm) {
// convert URL encoded seoterm into product name
// lookup product by name
}
This route should be added before the default route.
NOTE: Every other page on your site cannot be on the root level anymore, i.e. /aboutus, /home, etc.
You would need to add a custom route to your RegisterRoutes function.
routes.MapRoute(
"ProductFriendly", // Route name
"{productId}", // URL with parameters
new { controller = "YourProductControllerName", action = "YourProductActionName" } // Parameter defaults
);
Would map to an action in a YourProductControllerName called YourProductActionName
public ActionResult YourProductActionName(string productId)
{
// your code goes here...
}

MVC .NET 4 MapRoute + ActionLink or RouteLink issue

Okay, so here's the deal. I've got controller called "Hotel" with view called "Index", where I'm trying to produce code allowing me to generate links in form of:
../Hotel?id=1
with ID passed as argument. To do so, I've tried using MapRoute:
#Html.RouteCollection.MapRoute("Hotel", "../{controller}/{id}", new { controller = "hotel" });
together with ActionLink:
#Html.ActionLink("More >>>", "", "Hotel", new { id = item.HotelId }, null)
But the outcome link goes like this:
Hotel/Index/1
Which leads to correct location, but burns visual consistency of all links at my website. I've tried RouteLink as well, but with no success.
Thanks in advance!
Do you want to make all links in the application use the standard querystring format for parameters named "id"? If so, removing the {id} from url and defaults object in the "Default" route should do that for you just fine.
If you want to limit it to the "Hotel" controller, you are on the right track w/ the custom route. First, make sure the custom route comes before the default route definition, and second, define it with nothing beyond the controller/action like:
routes.MapRoute(
"HotelRoute", // Route name
"Hotel/{action}/", // URL with parameters
new { controller="Hotel", action = "Index" } // Parameter defaults
);
Any params you pass in should then be appended to the query string as a name/value pair.

Can't get MVC Routing to work

I have a site and the url looks like this;
Home/LocationPage?locationId=f25a9ba4-54dc-4e6a-bdbf-094a5a6f7801
What I would like it to look like is;
Home/My Restaurant
I've tried mapping routes like this;
routes.MapRoute(
"Location",
"{Controller}/{LocationName}",
new { controller = "Home", action = "LocationPage", LocationName = "" }
But can't seem to get the URL to change at all.
Keep the route you just made, but you may need to add in a couple more things.
Add in a route constraint (or override the OnActionExecuting method on the controller or add a attribute to the action method) that looks up the the LocationName route parameter in the database and (if found) adds a locationId parameter to the route parameters. Then create an action called LocationPage that takes a locationId (location Name is optional).
That way you get nice urls coming in, but the action doesn't need to do the lookups.
If you receive a parameter called LocationId in your Action then you need to change your route:
routes.MapRoute(
"Location",
"{Controller}/{LocationId}",
new { controller = "Home", action = "LocationPage", LocationId = "" }
And don't forget that in this way you wouldn't get this parameter as Guid then you should make your logic to retrieve the Guid value by the provided name.

MVC Route parameters

If I have this route:
routes.MapRoute(
"BlogRoute", // Route name
"blog/{action}", // URL with parameters
new { controller = "Blog", action = "Index", id="abc" } // Parameter defaults
);
... and have this Index method in the controller:
public ActionResult Index(string id)
{
return View((object)id);
}
Is it possible for someone to change that id parameter from "abc" to something else? For example, by appending ?id=somethingElse to the URL? I tried that but it didn't change it. So is it guaranteed that I'll always get "abc" in the Index method?
Basically I need to send a hardcoded string when one route is chosen and I don't want the user to be able to change this string via the URL or any other mechanism. It's like "abc" is a password (it's not but just assume it is). Only the developer is allowed to set this string by editing Global.asax.cs.
Is it possible?
You can add a constraint for the id parameter using the regular expression /abc/

problem with routing asp.net

I am fairly new to asp.net and I have a starting URL of http://localhost:61431/WebSuds/Suds/Welcome and routing code
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{page}", // URL with parameters
new { controller = "Suds", action = "Welcome", page = 1 } // Parameter defaults
);
routes.MapRoute(
"Single",
"{controller}/{action}",
new { controller = "Suds", action = "Welcome" }
);
I am receiving the following error: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Can anyone please help me figure out how to route the beginning url to the controller.
Because the first part of your URL is WebSuds, the framework is trying to map the request to WebSudsController instead of SudsController. One thing you can try is to change the url parameter of your route to "WebSuds/{controller}/{action}".
Route tables are searched on a first come first served basis. Once an appropriate match is found any following routes will be ignored.
You need to add "WebSuds/{controller}/{action}" and put it above the default route. The most specific routes should always go above the more generic ones.
With the following Routes and Url you have given make sure you have these Methods in your Controller
public class SudsController
{
public ActionResultWelcome(int? page)
{
return View();
}
}
Since you have created two routes of the same Action, one with a parameter page and another without. So I made the parameter int nullable. If you don't make your parameter in Welcome nullable it will return an error since Welcome is accepting an int and it will always look for a Url looks like this, /WebSuds/Suds/Welcome/1. You can also make your parameter as string to make your parameter nullable.
Then you should have this in your View Folder
Views
Suds
Welcome.aspx
If does doesn't exist, it will return a 404 error since you don't have a corresponding page in your Welcome ActionResult.
If all of this including what BritishDeveloper said. This should help you solve your problem.

Resources