如何让 Html.ActionLink 返回非查询字符串链接?

发布于 2024-12-21 15:50:55 字数 349 浏览 1 评论 0原文

每当我使用 Html.ActionLink 它总是给我参数作为?而不是将其作为 url 的一部分。

例如:

@Html.ActionLink("Edit", "Edit", new { Username = (string)item.username })

给我:

/Edit?Username=username

而不是:

/Edit/Username 

就像我在控制器中定义的那样。

谁能告诉我如何走另一条路?而不是的方式。

谢谢

whenever I use Html.ActionLink it always gives me the parameter as ? instead of it being apart of the url.

For example:

@Html.ActionLink("Edit", "Edit", new { Username = (string)item.username })

Gives me:

/Edit?Username=username

Instead of:

/Edit/Username 

like I have it defined in my controller.

Can anyone tell me how to get the other way? The instead of way.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-12-28 15:50:55

您可能需要在 global.asax 中注册一个路由来定义您的 Username 参数:

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

    routes.MapRoute(
        "EditStuff",
        "{controller}/{action}/{Username}",
        new { controller = "YourControllerName", action = "Edit" }
    );

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

}

You probably need to register a route in global.asax to define your Username parameter:

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

    routes.MapRoute(
        "EditStuff",
        "{controller}/{action}/{Username}",
        new { controller = "YourControllerName", action = "Edit" }
    );

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

}
我是男神闪亮亮 2024-12-28 15:50:55

我使用 AttributeRouting 来处理这些事情,用它你可以用实际路径装饰你的控制器,如下所示:

[GET("/Edit/{Username}")]
public ActionResult Edit(string Username)

I use AttributeRouting for these things, with it you can decorate your controller with the actual path, like so:

[GET("/Edit/{Username}")]
public ActionResult Edit(string Username)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文