ActionLink 行为不一致

ActionLink not behaving consistently

我有两个控制器,每个控制器都有一个使用路由属性的动作。在我看来,我使用 @Html.ActionLink 助手来为每个动作呈现 links。但是,其中一个操作未能呈现正确的 url。

这是我目前的情况:

查看

<div class="actionRow row">
    <div class="col-lg-12">
        @Html.ActionLink("Edit", "EditRequest", "Modelling", new { @requestId = Model.RequestId }, new { @class = "btn btn-danger" })
        @Html.ActionLink("Cancel", "Details", "Business", new { @pRef = Model.PRef }, new { @class = "btn btn-info" })
    </div>
</div>

建模控制器

public class ModellingController : Controller
{
    [HttpGet]
    [Route("modelling/{requestId}")]
    public ActionResult EditRequest(int requestId)
    {
    }
}

业务主管

public class BusinessController : Controller
{
    [HttpGet]
    [Route("business/details-card/{pRef}")]
    public ActionResult Details(string pRef)
    {
    }
}

结果

以这种方式,我在页面上生成了以下 urls: http://localhost:8061/modelling/3211 http://localhost:8061/business/details?pRef=bds1234

第一个 link 是正确的,但是我希望第二个 link 看起来像 http://localhost:8061/business/details-card/bds1234

更多详情

如果我将视图更改为具有以下标记,那么我确实会生成正确的 link,但它没有样式化 - 原因很明显。但是,如果我随后尝试添加 class 详细信息,它们将作为查询字符串参数附加到 link。

改变的观点

<div class="actionRow row">
    <div class="col-lg-12">
        @Html.ActionLink("Edit", "EditRequest", "Modelling", new { @requestId = Model.RequestId }, new { @class = "btn btn-danger" })
        @Html.ActionLink("Cancel", "Details", "Business", new { }, new { @pRef = Model.PRef })
    </div>
</div>

产量http://localhost:8061/business/details-card/bds1324

进一步改变观点

<div class="actionRow row">
    <div class="col-lg-12">
        @Html.ActionLink("Edit", "EditRequest", "Modelling", new { @requestId = Model.RequestId }, new { @class = "btn btn-danger" })
        @Html.ActionLink("Cancel", "Details", "Business", new { @class = "btn btn-info" } new { @pRef = Model.PRef })
    </div>
</div>

产量http://localhost:8061/business/details-card/bds1234?class=btn btn-info

更多信息

没有其他控制器,控制器中也没有其他操作,我在 route.config

中正确启用了地图属性路由

在你的情况下,它有助于添加:

routes.MapMvcAttributeRoutes();

在路由配置中:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

在视图中:

@Html.ActionLink("Edit", "EditRequest", "Modelling", new { @requestId = 1 }, new { @class = "btn btn-danger" })
@Html.ActionLink("Cancel", "Details", "Business", new { @pRef = "123" }, new {@class = ""})