mvc url.action返回null或url作为get [英] mvc url.action returning null or url as get

查看:145
本文介绍了mvc url.action返回null或url作为get的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mvc Url.Action

I am using mvc Url.Action as this

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

}

我无法获取要映射到此路线的网址.我得到的是

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

我以Url的身份获得

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

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

I cannot figure the why so of this behavior. Just started using .net mvc. Please help me with this.

推荐答案

好,所以想出要使Url.Action生成url要求在控制器中存在that动作,并且参数也应该存在.所以我只是将控制器操作更改为

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

这篇关于mvc url.action返回null或url作为get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆