路由难度

发布于 2024-09-03 04:02:06 字数 1203 浏览 1 评论 0原文

我的应用程序的一部分将存储在多个位置的资源映射到 Web URL,如下所示:

http://servername/Issue.aspx/Details?issueID=1504/productId=2345

是否可以构造一个与此匹配的 MVC 路由,以便我将完整的路径传递到我的控制器中?可以作为单个字符串,也可以作为 params 样式的字符串数组。

在我的 Global.aspx 中我有 路线.MapRoute( “问题”, “问题/{详细信息}”, new {controller =“问题”,action =“详细信息”}, 新 { 问题 ID = @"\d+", 产品 ID = @"\d+" } );

我已经尝试过代码

 RouteValueDictionary parameters = new RouteValueDictionary { {"Controller", "Issue"},{ "action", "Details" }, { "issueId", Test.ID }, {"productId", Test.Project.ID} };
        VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, parameters);
        var test =  vpd.VirtualPath;

测试值是

/Issue.aspx/Details?issueId=1504&productId=3625。

如何使用 ASP.NET 路由生成 URL 并将其发送给用户,他们应该能够通过单击生成的链接来打开页面。但是,这里不包括服务器名称。如何获得链接为 http://servername/Issue 的服务器名称.aspx/详细信息?issueID=1504/productId=2345

Part of my application maps resources stored in a number of locations onto web URLs like this:

http://servername/Issue.aspx/Details?issueID=1504/productId=2345

Is it possible to construct an MVC route that matches this so that I get the path in its entirety passed into my controller? Either as a single string or possibly as an params style array of strings.

In my Global.aspx I have
routes.MapRoute(
"Issue",
"Issue/{Details}",
new { controller = "Issue", action = "Details" },
new { issueId = @"\d+", productId = @"\d+" }
);

I have tried the code

 RouteValueDictionary parameters = new RouteValueDictionary { {"Controller", "Issue"},{ "action", "Details" }, { "issueId", Test.ID }, {"productId", Test.Project.ID} };
        VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, parameters);
        var test =  vpd.VirtualPath;

test value is

/Issue.aspx/Details?issueId=1504&productId=3625.

How to generate URLs Using ASP.NET Routing and sends it to users and they should be able to open the page by clicking on the generated link. However, here the servername isn`t included. How can I have the servername with the the link as http://servername/Issue.aspx/Details?issueID=1504/productId=2345

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

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

发布评论

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

评论(2

天邊彩虹 2024-09-10 04:02:06

首先,您错误注册了路线。 Issue/{Details} 构建一个名为 Details 的参数,并提供您在 url 中的 Issue/ 之后输入的值。我猜你应该删除大括号。
理想情况下,我认为您应该编写如下内容:

routes.MapRoute( 
"Issue", //route name (optional)
"Issue/Details/{issueId}/{productId}/", //format
new { controller = "Issue", action = "Details", issueId=0, productId=0 }, //default values 
new { issueId = @"\d+", productId = @"\d+" }  //validation
);

如果您正确设置了路线,请使用它在视图中生成链接,

<%= Html.ActionLink("Click me", "MyAction", "MyController", Request.Url.Scheme,
Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port), "anchorName", new { param = "value" },
new { myattribute = "something" }) %>

它将生成类似的内容

<a myattribute="something"
href="https://www.example.com/MyController/MyAction?param=value#anchorName">
Click me</a>

我推荐 这本书如果你想更好地了解 ASP.NET MVC 平台。我这周只读了一次,就能够回答这个问题了。

First of all you misregistered the route. Issue/{Details} builds a parameter called Details, and gives a value you enter after Issue/ in your url. I am guessing you should remove the curly brackets.
Ideally, I think you should write something like this:

routes.MapRoute( 
"Issue", //route name (optional)
"Issue/Details/{issueId}/{productId}/", //format
new { controller = "Issue", action = "Details", issueId=0, productId=0 }, //default values 
new { issueId = @"\d+", productId = @"\d+" }  //validation
);

If you have the route set up correctly, use this to generate the link in your View

<%= Html.ActionLink("Click me", "MyAction", "MyController", Request.Url.Scheme,
Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port), "anchorName", new { param = "value" },
new { myattribute = "something" }) %>

it will generate something like this

<a myattribute="something"
href="https://www.example.com/MyController/MyAction?param=value#anchorName">
Click me</a>

I recommend this book if you want to know the ASP.NET MVC platform better. I've only read it once this week and I was able to answer this.

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