【问题标题】:Url routing like facebook asp.net像 facebook asp.net 这样的 URL 路由
【发布时间】:2014-12-02 16:45:44
【问题描述】:

我是 asp.net 新手,目前正在学习 url 路由

在 facebook 链接中(https://www.facebook.com/james.woodjames.wood 是如何工作的?

我尝试在我的 asp.net 中使用添加到我的全局中的这段代码来做同样的事情

route.MapPageRoute("Profile", "epubtest/profile/{profileid}", "~/epubtest/Profile.aspx");

但我无法让它与 dot 一起使用,例如 james.wood

点是指另一个参数还是只是一个参数?

它没有一个点,只有一个单词

【问题讨论】:

  • 尝试使用点会发生什么?
  • @haim770 它将注册为一个新页面。我得到了HTTP Error 404.0 - Not Found
  • 它可能将.wood 视为扩展名

标签: c# asp.net routing


【解决方案1】:

1) 你可以添加这条路线:

routes.MapRoute(
    name: "User",
    url: "{username}",
    defaults: new { controller = "Destiny", action = "Index" },
    constraints: new { username = new UserNameConstraint() }
);

2) 创建这个类:

public class UserNameConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        List<string> users = new List<string>() { "username1", "username2" };

        var username = values["username"].ToString().ToLower();

        return users.Any(x => x.ToLower() == username);
    }
}

3) 命运控制器

public class DestinyController : Controller
{
    public ActionResult Index(string username)
    {
        return View();
    }
}

我希望我有所帮助。 拥抱!

【讨论】:

    猜你喜欢
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多