ASP.NET“友好的URL”模块 - 不工作的根URL [英] ASP.NET 'Friendly URL' module - not working for root URL

查看:168
本文介绍了ASP.NET“友好的URL”模块 - 不工作的根URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地用友好的URL模块中的ASP.NET 4.5

在航线配置,我可以添加这样的路线:

  routes.MapPageRoute(我的空间,我的空间/ {} mypageName,〜/ mypage.aspx);

像这样的网址:

mysite.com/mypage/hello

在网页mypage.aspx我能得到URL段是这样的:

 使用Microsoft.AspNet.FriendlyUrls;//获取URL段
IList的<串GT;段= Request.GetFriendlyUrlSegments();
如果(segments.Count大于0)
{
    //获取第一段
    字符串的URL =段[0];}

不过,我不能得到这个工作的根URL的。例如我site.com/ttee

我想获得ttee'并把它传递到页面。但Request.GetFriendlyUrlSegments()'根返回0。

我如何最好地做到这一点?


解决方案

  routes.MapPageRoute(我的空间,我的空间/ {} mypageName,〜/ mypage.aspx) ;

这将只对这种格式的URL:

  www.example.com/mypage/changingparthere

如果你想它

  www.example.com/changablemypage

将其设置为:

  routes.MapPageRoute(我的空间,{} mypageName,〜/ mypage.aspx);

但正如你所看到的,它会抓住所有的一切。所以一定要确保它是在Global.asax中的最后一个路由。

I am successfully using the 'Friendly URL' module in ASP.NET 4.5

In route config I can add a route like this:

routes.MapPageRoute("mypage", "mypage/{mypageName}", "~/mypage.aspx");

for a URL like this:

mysite.com/mypage/hello

in the page 'mypage.aspx' I can get URL segments like this:

using Microsoft.AspNet.FriendlyUrls;

// Get URL segments
IList<string> segments = Request.GetFriendlyUrlSegments();
if (segments.Count > 0)
{
    // Get first segment
    string url = segments[0];



}

However, I cannot get this working for root URL's. e.g. 'my site.com/ttee'

I want to get 'ttee' and pass it into a page. But 'Request.GetFriendlyUrlSegments()' returns 0 for the root.

How best can I do this?

解决方案

routes.MapPageRoute("mypage", "mypage/{mypageName}", "~/mypage.aspx");

This will work only for URLs in this format:

www.example.com/mypage/changingparthere

If you want to make it

www.example.com/changablemypage

Set it to:

routes.MapPageRoute("mypage", "{mypageName}", "~/mypage.aspx");

But as you can see, it will catch literally everything. So make sure it is the last routing on Global.asax.

这篇关于ASP.NET“友好的URL”模块 - 不工作的根URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆