ASP.NET 4.0 Routing and Subfolders

谁都会走 提交于 2019-12-05 07:01:45

问题


I have a folder structure like this:

www.mysite.com/About/About.aspx

I have a link in a user control like this:

<a href="~/About/About" id="aboutLink" title="About" runat="server">About</a>

And in my RegisterRoutes() method, I have this:

routes.MapPageRoute("", "About/About/", "~/About/About.aspx");

It works but produces the following URL:

www.mysite.com/About/About

What I would like is this:

www.mysite.com/About

Is this possible with out-of-the-box 4.0 routing?

UPDATE 2 - 05-14-2010:

Apparently, I introduced an extra issue by naming the .aspx Web Form the same as the containing folder. Finally, this is what worked for me:

RouteTable.Routes.MapPageRoute("", "About/", "~/AboutUs/About.aspx");

<asp:HyperLink ID="aboutLink" NavigateUrl="~/About" 
    Text="About" runat="server"></asp:HyperLink>

The links provided by Raj helped me find the answer :-)
http://msdn.microsoft.com/en-us/library/cc668201.aspx


回答1:


Your question is not clear to me. Try this

routes.MapPageRoute("", "About/", "~/About/About.aspx"); 

Also, consider using RouteURl expressions.

http://msdn.microsoft.com/en-us/library/dd329551.aspx

http://msdn.microsoft.com/en-us/library/cc668176.aspx




回答2:


The second parameter is how the URL will look and accessing the virtual page will do the trick

// Global.asax
void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    System.Web.Routing.RouteTable.Routes.MapPageRoute("", "About", "~/About/About.aspx");
}


<a href="/About" id="aboutLink" title="About" runat="server">About</a>



回答3:


Your a tag links to /About/About, so, naturally, that's what you get. Did you try to make it point to /About instead? (and correspondingly change the route definition)

Or do I misunderstand the question?



来源:https://stackoverflow.com/questions/2831292/asp-net-4-0-routing-and-subfolders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!