views:

65

answers:

2

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

A: 

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"&gt;
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.

Alexander
Thanks for replying. I have not well understood the explanation. How will I be able to generate the URL and send the link via email so that the page opens when the user clicks on the link?
Like I said:1. Define the route correctly2. Use the HTML Helper called ActionLink to generate the linkIt doesn't matter really what you do with the ViewResult. You can either return it as a HTML page, or send it via Email. This question is about routing, not how to transmit mail or how to fetch HTML from a view and use it later on. If you are seeking answers to Those questions, create a new question here or something. I won't be able to answer them right now.
Alexander
I would like to have the virtual path. How can I have the vitual path? I have used VirtualPathUtility.ToAbsolute(HttpContext.Current.Request.ApplicationPath); but it`s returning "\". I want to generate something like http://localhost:4219/Issue.aspx/Details?issueID=1504. I am doing that in my model, so I guess I won`t be able to use Actionlink?
Models are strictly for logic. You don't generate content in your model. You only generate and work data.ActionLink, though, is just a static method, an extension for the Html class, which can be used almost from anywhere.But, in an MVC pattern you should avoid output inside models (even if it's sent via mail). The whole point in MVC is dividing the entities. Only logic goes into models, and all that has to do with HTML, presentation, etc. goes inside Views, while controllers glues them both together.
Alexander
Like I said, MVC is a pattern. You either stick to it strictly, make the code easily maintainable, extensible, or mix HTML, SQL and C# in a single file, making something that is called an Anti-Pattern, or Smart UI and lose all the advantages of MVC. Even for the simplest tasks you SHOULD divide everything between Models, Views and Controllers, otherwise you end up abandoning the MVC concept, and you lose the reason to write in ASP.NET MVC instead of regular ASP.NET
Alexander
There is something known as urlhelper. Can you please tell me how can I use it to solve this particular problem?