mvc url.action returning null or url as get

2019-09-08 03:17发布

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条回答
我命由我不由天
2楼-- · 2019-09-08 04:19

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();
}
查看更多
登录 后发表回答