开发者

routing difficulty

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 开发者_JAVA百科isn`t included. How can I have the servername with the the link as http://servername/Issue.aspx/Details?issueID=1504/productId=2345


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.


http://weblogs.asp.net/srkirkland/archive/2009/09/17/a-urlhelper-extension-for-creating-absolute-action-paths-in-asp-net-mvc.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜