问题
I am using mvc Url.Action as this
<a href="@Url.Action("Index", "Product", new { area = "Product", search = UrlParameter.Optional, categoryId = category.IdCategory })">
I have my routing as:-
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Product_default",
"Product/{controller}/{action}/{id}/{categoryId}",
defaults: new { controller = "Product", action = "Index", search = UrlParameter.Optional, categoryId = @"\d+" },
namespaces: new[] { "IBuyFrontEnd.Areas.Product.Controllers" }
);
}
I cannot get the url to map to this route. I am getting it as
However if i change the action to
@Url.Action("Index", "Product")
I get this as Url
http://localhost/iteric/?action=Index&controller=Product
I cannot figure the why so of this behavior. Just started using .net mvc. Please help me with this.
回答1:
Ok So figured out that to get Url.Action to generate the url requires the that action to be present in the controller, and also the parameters should be in place. So i just changed my controller action to
public ActionResult Index( int? categoryId,string search)
{
return View();
}
来源:https://stackoverflow.com/questions/34479565/mvc-url-action-returning-null-or-url-as-get