UrlHelper.Action(QUOT;编辑"," AD")返回id参数? [英] UrlHelper.Action("Edit", "Ad") returns id parameter?

查看:227
本文介绍了UrlHelper.Action(QUOT;编辑"," AD")返回id参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code生成一个网址:

  UrlHelper urlHelper =新UrlHelper(htmlHelper.ViewContext.RequestContext);
urlHelper.Action(编辑,广告);

如果即时通讯目前正在URL 的http://本地主机:16055 /广告/编辑/ 87 操作方法将返回:/广告/编辑/ 87

为什么呢?我认为urlHelper.Action(编辑,广告),将在此satet不包括任何参数?

BestRegards

编辑1:(击溃)

  routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);    routes.MapRoute(
        默认,//路线名称
        {控制器} / {行动} / {ID},// URL带参数
        新{控制器=广告,行动=名单,ID = UrlParameter.Optional} //参数默认
    );    routes.MapRoute(
        TreeEditing,//路线名称
        {控制器} / {行动} / {名} / {ID},// URL带参数
        新{控制器=AdCategory,行动=添加,名称=的String.Empty,ID = -1}
    );


解决方案

  

我认为urlHelper.Action(编辑,广告),将在此satet不包括任何参数?


那么,你想错了。所有网址助手自动包括是原始请求的一部分的所有参数。所以,如果你有一个ID的路由参数present,它的价值将被重新使用。

如果您不希望这种行为,你将不得不对这些参数明确设置的值:

  VAR行动= urlHelper.Action(编辑,广告,新{ID =});

Hi,

I am using the following code to generate a URL :

UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
urlHelper.Action("Edit", "Ad");

If Im currently is on URL http://localhost:16055/Ad/Edit/87 the Action method will return : "/Ad/Edit/87" ?

Why? I thought that urlHelper.Action("Edit", "Ad") would in this satet not include any parameters?

BestRegards

Edit 1: (routs)

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Ad", action = "List", id = UrlParameter.Optional } // Parameter defaults
    );

    routes.MapRoute(
        "TreeEditing", // Route name
        "{controller}/{action}/{name}/{id}", // URL with parameters
        new { controller = "AdCategory", action = "Add", name = string.Empty, id = -1 }
    );

解决方案

I thought that urlHelper.Action("Edit", "Ad") would in this satet not include any parameters?

Well, you thought wrong. All url helpers automatically include all arguments that were part of the original request. So if you had an id route parameter present, its value will be reused.

If you don't want this behavior you will have to explicitly set the values for those parameters:

var action = urlHelper.Action("Edit", "Ad", new { id = "" });

这篇关于UrlHelper.Action(QUOT;编辑"," AD")返回id参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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