问题
i am trying to create route.
Which is
/emlak/TITLE/number.aspx
such as
/emlak/Here_is_your_best_property/123456.aspx
Global.asax:
routes.MapRoute(
"Product",
"{controller}/{deli}/{productId}",
new { controller = "emlak", action = "Index" },
new { productId = UrlParameter.Optional , deli = UrlParameter.Optional }
);
My controller
namespace emrex.Controllers
{
public class EmlakController : Controller
{
//
// GET: /Emlak/
public ActionResult Index(String productId, String deli)
{
return View();
}
}
}
and i am getting next error:
Server Error in '/' Application.
The resource cannot be found.
Thanks for help.
回答1:
Your problem is (at least when I tried your code) you have route constraints specified where they really shouldn't be. I was able to get this to work just fine by doing:
routes.MapRoute( "Product", "{controller}/{deli}/{productId}", new { controller = "emlak", action = "Index", productId = UrlParameter.Optional, deli = UrlParameter.Optional } );
Try that - any difference?
回答2:
Don't provide URL parameter defaults as constraints (as you did)
When you define your route as (I added additional comments so we know what each part is)
routes.MapRoute(
// route name
"Product",
// Route URL definition
"{controller}/{deli}/{productId}",
// route values defaults
new { controller = "emlak", action = "Index" },
// route values constraints
new { productId = UrlParameter.Optional , deli = UrlParameter.Optional }
);
So basically you shouldn't provide constraints in your case which makes it meaningless. Put the last two in route defaults and keep constraints out of this route defintion as:
routes.MapRoute(
"Product",
"{controller}/{deli}/{productId}",
new {
controller = "Emlak",
action = "Index",
productId = UrlParameter.Optional,
deli = UrlParameter.Optional
}
);
This should definitely work unless you have some other route definitions or don't use code that you provided.
回答3:
This may help, as I haven't upgraded from MVC 1.0 yet...
I don't think you need the .aspx portion of the URL because MVC handles application instantiation differently. Also you need a .mvc extension if using IIS 6 (e.g. "emlak.mvc/TITLE/number"); IIS 7 should instantiate correctly with "emlak/TITLE/number".
回答4:
You should remove the constraints and provide the deafults for "productId" and "deli".
routes.MapRoute(
"Product",
"{controller}/{deli}/{productId}",
new { controller = "emlak", action = "Index", productId = 123 , deli = "xyz" }
);
OR
make your parameters optional at action in your controller
public ActionResult Index(String productId = 0, String deli = "")
{
return View();
}
回答5:
Your action requires that deli and productId both be supplied, and your route does not supply default values for either. Either add an Index action which does not require any values be supplied, or add default values for your variables.
counsellorben
来源:https://stackoverflow.com/questions/5756510/asp-net-mvc-3-routing