mvc url.action returning null or url as get

白昼怎懂夜的黑 提交于 2019-12-12 03:07:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!