Unsure about parameter - asp.net-mvc

I am currently working on a beginner's MVC tutorial. I was wondering if anyone could explain how or where the parameters of this method are chosen?
public ActionResult Details(int id)
{
var album = storeDB.Albums.Find(id);
return View(album);
}

There are 2 ways your id parameter could be populated:
http://www.example.com/{Controller}/Details/{id}
or
http://www.example.com/{Controller}/Details?id={id}
where {Controller} is the name of your Controller, eg. The name of HomeController.cs would be "Home"
and where {id} is an int.

you're working with the default route I guess, so you gonna find in the Global.asax file the follow code:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
your route is that! where "id" is a optional parameter, suppose that your controller name is Album, so test http://mySite/Album/Details/10
you get a request where 10 is your Id parameter specify on the action Details

Related

cant find Route when action has 2 params - Asp.Net MVC

I have a controller named Blog.
I have an action like this:
[Route("{code:int}/{title?}")]
public virtual ActionResult Index(int code, string title)
{
var postModel = _blogService.Get(code.ToUrlDecription());
return View(postModel);
}
I entered these urls, but all of them returned not found:
localhost:7708/Blog/index/12/post-title;
localhost:7708/Blog/index/12;
localhost:7708/Blog/12/post-title.
I tried to write a route like below, but the result was the same:
routes.MapRoute(
name: "showblogpost", url: "{controller}/{action}/{code}/{title}",
defaults: new {
controller = "Blog",
action = "Index",
title = UrlParameter.Optional
},
namespaces:new string[] { "Web.Controllers" }
);
One thing, you don't need to use both attribute [Route] on action and mapping route.
In your attribute [Route] you have specified only parameters, so route according to it should be localhost:7708/12
by route, specified in MapRoute it should be localhost:7708/showblogpost/12
What I suggest is - remove your attribute, name your route in MapRoute as you want to see in URL, and also you can remove "string title" parameter from action, as it's not used.

asp.net mvc get route corresponding url

I faced with the following problem. I have an url and I would like to check whether the url could be mapped to any route defined in my RouteConfig or not. In other words I need a right way to determine whether the url is correct for my application or not.
Maybe anyone knows how to do it?
For Example : Student is my contoller and having action method StudentList then I need to write following code in RouteConfig.cs file.
StudentContoller.cs
public List<Student> StudentList (StudentModel model)
{
/* Logic for Student List */
return View(model);
}
RouteConfig.cs
routes.MapRoute(
name: "StudentList",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Student", action ="StudentList", id = UrlParameter.Optional }
);

Can I make a route without the action for only one action?

Here's an example of my set up:
public class UserController : Controller
{
public ActionResult Index(int? id) { ... }
[HttpPost]
public ActionResult DoSomething(int id) { ... }
public ActionResult Search([params]) { ... }
}
and I want to be able to access them via these routes:
/app/User/{id}
/app/User/DoSomething/{id}
/app/User/Search/
I tried setting up my routes like this, but then if I try to navigate to /app/User/Search/ or post to /app/User/DoSomething/, the Index Action is hit instead.
routes.MapRoute(
name: "UserWithoutIndex",
url: "User/{id}",
defaults: new { controller = "User", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
How can I do this? I think it would work to just specify each specific action in it's own route before the UserWithoutIndex route above, but I have multiple actions and I don't want to have to create a route specific to each action in the controller.
The problem is your first route will match any two-segment URL which includes the examples you provided; /app/User/Search/ and /app/User/DoSomething/ and the values Search and DoSomething will be placed in the id place holder respectively. Then because the first route is being matched you are receiving Index for the action. If your id will take on some format specifically you could specify a constraint for it in the first route like so:
routes.MapRoute(
name: "UserWithoutIndex",
url: "User/{id}",
defaults: new { controller = "User", action = "Index", id = UrlParameter.Optional },
constraints: new { id = "your regex here" }
);
If you constraint can be specific enough to the format of the id than things like Search and DoSomething won't match and the route won't match so the next route will be tried.
Also, if there will always be an id specified in the scenarios where you want the first route to be targeted you should remove the id = UrlParameter.Optional default so that way the id will be required and the route will ONLY match two-segment URLs because as it is now with the id being optional the route will also match one-segment URLs.

URL Routing parameter name, order

Asking for the best way to address this issue:
in my controller I have the following action
public ActionResult Member(string id){return View();}
another action in the same controller
public ActionResult Archive(string year){return View();}
in my global.asax
routes.MapRoute(
"Archive", // Route name
"{controller}/{action}/{year}", // URL with parameters
new { controller = "Home", action = "Index", year = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id="" } // Parameter defaults
);
if I call this url mysite.com/archive/2009 this works because the action expecting parameter with a name "year" and the first route schema works with this request. But if I call this url mysite.com/member/john it will result in this url mysite.com/member?id=john
So it looks like if my first route in global.asax have the same construction but different parameter name, the one with the right parameter name will have the right url (mysite.com/archive/2009) but for the other won't. How can I solve this issue? I can change the route table to expect a generic parameter name like "param", but I need to change all the signature of the action to "param" as well and it is not very descriptive (param instead year or param instead id).
Try this:
routes.MapRoute(
"Archive", // Route name
"Home/Member/{id}", // URL with parameters
new { controller = "Home", action = "Member", id = "" } //
Parameter defaults
);
routes.MapRoute(
"Archive", // Route name
"{controller}/{action}/{year}", // URL with parameters
new { controller = "Home", action = "Index", year = "" } // Parameter defaults
);
You are allowed to use literals in the second parameter, and they will act as filters. The more specific your route is, the closer you need to put it to the top of the route configuration, the routing system will choose the first route that matches.
Here is a link to more detailed background information. Some of the syntax has changed since the article was written but the basic rules seem up to date.

ASP.Net MVC 2 RC2: Custom route returns 404 when all optional parameters are null

I get a 404 error when I navigate to the following URL using the route below:
http://localhost:53999/properties/
However, all the following are correctly routed to the List action in my controller:
http://localhost:53999/properties/usa/new-york/manhattan/12
http://localhost:53999/properties/usa/new-york/manhattan
http://localhost:53999/properties/usa/new-york
http://localhost:53999/properties/usa
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//properties
routes.MapRoute(
"Properties",
"Properties/{country}/{state}/{city}/{id}",
new
{
controller = "Properties",
action = "List",
country = UrlParameter.Optional,
state = UrlParameter.Optional,
city = UrlParameter.Optional,
id = UrlParameter.Optional
}
);
//default
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
In PropertiesController.cs:
public ActionResult List(string country, string state, string city, string id)
{
return View();
}
Anyone know what I'm missing? Looks like it should just go to the default action, but it obviously doesn't...
You can also try the following (since you're using MVC 1.0).
Add a route above the current route:
routes.MapRoute(
"Properties", "Properties", new { controller = "Properties", action = "List"}
);
And add an overloaded ActionResult List() method to your controller:
public ActionResult List()
{
return View();
}
Have you tried this Route Debugger from Phil Haack? It may help you determine what is going on.
Instead of passing
(string)null
try passing
UrlParameter.Optional
as specified in Phil Haacks post here. I don't know if this will solve the issue as I'm not currently in a position to test it.
Brad Wilson answered it on this post: http://forums.asp.net/p/1527697/3690295.aspx#3690295
"No, the problem is that you have a Properties folder on your disk, so it's dispatching through the standard dispatcher (not MVC), and then 404ing because it can't find a default document."

Resources