不要在 URL 中显示操作 - 路由

发布于 2025-01-06 19:45:28 字数 1004 浏览 1 评论 0原文

我有一个带有 Index() ViewResult 的 DirectoryController,它在视图中显示以下链接:

@Html.ActionLink("My file 1", "Index", "File", new { @id = "123" }, null) <br />
@Html.ActionLink("My file 2", "Index", "File", new { @id = "456" }, null) <br />
@Html.ActionLink("My file 3", "Index", "File", new { @id = "789" }, null) <br />

如果我单击其中一个链接,我最终会在 URL 中看到(例如)以下内容:

http://www.domain.com/File/Index/123< /p>

我想要什么真正喜欢的是 URL 中的以下内容:

http://www.domain.com/File/my-file-1< /a>

基本上,我希望在 URL 中看到控制器名称和自定义名称(例如文件名),而不显示实际的操作id

无论我点击哪个链接,它们都应该继续指向 FileController 的 Index() 方法,并传递适当的id

我该怎么办呢?

我应该如何创建以及创建什么样的自定义路由?

谢谢

I have a DirectoryController with an Index() ViewResult which displays the following links in the view:

@Html.ActionLink("My file 1", "Index", "File", new { @id = "123" }, null) <br />
@Html.ActionLink("My file 2", "Index", "File", new { @id = "456" }, null) <br />
@Html.ActionLink("My file 3", "Index", "File", new { @id = "789" }, null) <br />

If I click on one of the links, I’ll end up having (for example) the following in the URL:

http://www.domain.com/File/Index/123

What I’d really like to have is the following in the URL:

http://www.domain.com/File/my-file-1

Basically, I’d like to see the Controller name and a custom name (the name of the file for example) in the URL without showing the actual action or the id.

Regardless of the link I click, all of them should continue to point to the Index() method of the FileController passing along the appropriate id.

How would I go about this?

How and what kind of custom route should I create?

Thanks

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

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

发布评论

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

评论(2

惟欲睡 2025-01-13 19:45:28

一般概述:

您想要做的是创建路由映射,

例如翻译用户友好的 url,

您需要在您的控制器/global.asax 中进行一些更新

的路线示例您将拥有类似的内容

  context.MapRoute(
              "FileNameMapping",
              "File/{filename}",
              new {controller="File", action = "Index" });

您的操作中

Public FileResult Index(string filename){
// do your logic for handleling file name 
//and return file
}

As general overview:

what you want to do is create route mapping

eg translating url that is user friendly

you would need to do some updates in your controller/ global.asax

example of route

  context.MapRoute(
              "FileNameMapping",
              "File/{filename}",
              new {controller="File", action = "Index" });

in your action you will have something like

Public FileResult Index(string filename){
// do your logic for handleling file name 
//and return file
}
病女 2025-01-13 19:45:28

如果您将在 IIS 而不是 Casini 上运行此程序,则可以使用 URL 重写来实现所需的结果。

In case you'll be running this on IIS, rather than Casini, you can use URL re-writes to achieve the desired result.

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