【问题标题】:mvc url.action returning null or url as getmvc url.action 返回 null 或 url 作为 get
【发布时间】:2015-12-27 10:31:19
【问题描述】:

我正在使用 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。我得到它作为

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

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

我把这个作为 URL

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

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

【问题讨论】:

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


    【解决方案1】:

    好的所以发现要让 Url.Action 生成 url 需要该操作存在于控制器中,并且参数也应该到位。所以我只是将我的控制器操作更改为

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

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 2020-02-01
      • 1970-01-01
      • 2015-07-24
      • 2015-05-02
      • 2021-12-24
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多