mvc url.action将null或url作为get返回

时间:2015-12-27 10:31:19

标签: asp.net-mvc-4 asp.net-mvc-routing

我正在使用mvc Url.Action作为此

<a href="@Url.Action("Index", "Product", new { area = "Product", search = UrlParameter.Optional, categoryId = category.IdCategory })">

我的路由为: -

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" }
    );

}

我无法将网址映射到此路线。我正在把它作为

但是,如果我将操作更改为

@Url.Action("Index", "Product")

我认为这是Url

http://localhost/iteric/?action=Index&controller=Product

我无法弄清楚这种行为的原因。刚开始使用.net mvc。请帮我解决一下这个。

1 个答案:

答案 0 :(得分:0)

确定因此,要想让Url.Action生成url,需要在控制器中存在该操作,并且参数应该就位。所以我只是将我的控制器动作改为

public ActionResult Index( int? categoryId,string search)
{
    return View();
}