Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ActionLink没有';不能使用URL进行更正,但RouteLink使用属性路由进行更正_C#_Asp.net Mvc 5_Asp.net Mvc Routing - Fatal编程技术网

C# ActionLink没有';不能使用URL进行更正,但RouteLink使用属性路由进行更正

C# ActionLink没有';不能使用URL进行更正,但RouteLink使用属性路由进行更正,c#,asp.net-mvc-5,asp.net-mvc-routing,C#,Asp.net Mvc 5,Asp.net Mvc Routing,我偶尔会让ActionLink拒绝在我的应用程序中使用占位符创建正确的URL,但当我在Route属性上指定Name时,它会生成URL属性 我最近一次与此相关的内容来自: [RoutePrefix("RuleConfiguration")] public class RuleConfigurationController : EntityController<RuleConfigurationModel, RuleConfiguration> { //...Snip...

我偶尔会让
ActionLink
拒绝在我的应用程序中使用占位符创建正确的URL,但当我在
Route
属性上指定
Name
时,它会生成URL属性

我最近一次与此相关的内容来自:

[RoutePrefix("RuleConfiguration")]
public class RuleConfigurationController : EntityController<RuleConfigurationModel, RuleConfiguration>
{
    //...Snip...
    [Route("{configurationId}/Edit", Name = "RuleConfigurationEdit")]
    public async Task<ActionResult> Edit(int configurationId)
    {
        return View(...);
    }
    //...Snip...
}
按照
/RuleConfiguration/1/Edit
给我一个url。然而,我得到的只是
/RuleConfiguration
。使用
RouteLink
时,如下所示:

@Html.RouteLink(conf.Name, "RuleConfigurationEdit", new { configurationId = conf.Id })
这将生成预期的URL。在同一个视图中,我有一个
ActionLink
生成到另一个操作的链接,该链接使用预期的操作名称生成。一个区别是,另一个操作没有任何路由参数

我已经检查了(几次)传递的参数
ActionLink
拼写是否正确,URL参数是否与函数期望的匹配。将
Name
参数添加到
Route
中,以查看
RouteLink
是否可以工作,
ActionLink
在之前或之后没有按预期工作


有没有什么我完全不懂的东西?

从第一个视图片段开始:

@Html.ActionLink(
    name,
    "Edit",
    "RuleConfiguration",
    new {
        configurationId = conf.Id
    }
)
此代码段使用方法的重载:

这将生成以下HTML:


configurationId
属性被添加到元素中,因为最后一个参数是
htmlAttributes
参数

我发现,如果当前请求已通过
RuleConfigurationController
路由,只需调用以下命令似乎就可以了(使用重载):

这将生成以下HTML:


但是,当尝试引用其他控制器中的操作时,这似乎不起作用


要将重载与要提供的参数一起使用,请使用以下选项之一:

如果未使用
htmlAttributes
参数,则可以将其传递给
null
,使代码段变为:

@Html.ActionLink(
    name,
    "Edit",
    "RuleConfiguration",
    new {
        configurationId = conf.Id
    },
    null
)
这将生成预期的锚元素,并且由于指定了控制器名称,因此在引用当前请求未通过其路由的控制器时,它也会起作用

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    String linkText,
    String actionName,
    Object routeValues,
    Object htmlAttributes
)
@Html.ActionLink(name, "Edit", new { configurationId = 1 })
public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    String linkText,
    String actionName,
    String controllerName,
    Object routeValues,
    Object htmlAttributes
)
@Html.ActionLink(
    name,
    "Edit",
    "RuleConfiguration",
    new {
        configurationId = conf.Id
    },
    null
)