如何在 MVC 3 中映射类似 (site.com/username) 的路由

发布于 2024-11-18 07:08:54 字数 117 浏览 3 评论 0原文

我想绘制这样的路线:

mysite.com/用户名

ASP.NET MVC 3 应用程序中的 ;请问我该怎么做? 谢谢大家,问候

I want to map a route like this:

mysite.com/username

in ASP.NET MVC 3 application; How can I do it, please?
Thanks to all, regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虚拟世界 2024-11-25 07:08:55

也许在一开始就添加从 System.Web.Routing.Route 继承的自定义路由,并覆盖

protected virtual bool ProcessConstraint(HttpContextBase httpContext, object constraint, 
   string parameterName, RouteValueDictionary values, RouteDirection routeDirection)

使用索引列检查数据库的方法。

只需添加一些不可为空的约束对象,

Constraints = new { username = "*" }

以便路由对象将处理约束。

在访问数据库之前,可以先试探一下这是否可以是用户名。请记住,您不能拥有与控制器相同的用户(当具有默认操作时),因此您希望以某种方式限制它。请注意这是否不会影响年度表现,但斜线后可能没有很多具有单一值的路线。

您可以这样做。

public class UserRoute : Route
{
    public UserRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
        : base(url, defaults, constraints, routeHandler)
    {
    }

    protected override bool ProcessConstraint(HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (routeDirection == RouteDirection.UrlGeneration)
            return true;

        object value;
        values.TryGetValue(parameterName, out value);
        string input = Convert.ToString(value, CultureInfo.InvariantCulture);
        // check db
        if (!CheckHeuristicIfCanBeUserName(input))
        {
            return false;
        }

        // check in db if exists, 
        // if yes then return true
        // if not return false - so that processing of routing goes further
        return new[] {"Javad_Amiry", "llapinski"}.Contains(input);
    }

    private bool CheckHeuristicIfCanBeUserName(string input)
    {
        return !string.IsNullOrEmpty(input);
    }
}

这个加在开头。

routes.Add(new UserRoute("{username}", 
            new RouteValueDictionary(){ {"controller", "Home"}, {"action", "About"}}, 
            new RouteValueDictionary(){ { "username", "*" }}, new MvcRouteHandler()));

对我来说有两种方式:产生和接收。

Maybe add custom route at the very beginning that would inherit from System.Web.Routing.Route, and override method

protected virtual bool ProcessConstraint(HttpContextBase httpContext, object constraint, 
   string parameterName, RouteValueDictionary values, RouteDirection routeDirection)

where you would check in db using indexed column.

Just add some not nullable constraint object like

Constraints = new { username = "*" }

so that route object will process constraints.

Before hitting database maybe make some heuristics whether this can be username. Remember you cant have user same as controller (when having default action), so you want to limit that somehow. Pay attention if that wont hurt year performance, but you probably dont have many routes with single value after slash.

This is how you can do this.

public class UserRoute : Route
{
    public UserRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
        : base(url, defaults, constraints, routeHandler)
    {
    }

    protected override bool ProcessConstraint(HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (routeDirection == RouteDirection.UrlGeneration)
            return true;

        object value;
        values.TryGetValue(parameterName, out value);
        string input = Convert.ToString(value, CultureInfo.InvariantCulture);
        // check db
        if (!CheckHeuristicIfCanBeUserName(input))
        {
            return false;
        }

        // check in db if exists, 
        // if yes then return true
        // if not return false - so that processing of routing goes further
        return new[] {"Javad_Amiry", "llapinski"}.Contains(input);
    }

    private bool CheckHeuristicIfCanBeUserName(string input)
    {
        return !string.IsNullOrEmpty(input);
    }
}

This add at the beginning.

routes.Add(new UserRoute("{username}", 
            new RouteValueDictionary(){ {"controller", "Home"}, {"action", "About"}}, 
            new RouteValueDictionary(){ { "username", "*" }}, new MvcRouteHandler()));

Works for me in both ways, generating and incomming.

£噩梦荏苒 2024-11-25 07:08:55

也许是这样的?

routes.MapRoute(
    "Profile",
    "{username}",
    new { controller = "Profile", action = "View" }
);

当你的控制器是

public class ProfileController : Controller {

    public ActionResult View(string username) {
       // ...
    }

}

Maybe something like this?

routes.MapRoute(
    "Profile",
    "{username}",
    new { controller = "Profile", action = "View" }
);

With your controller being

public class ProfileController : Controller {

    public ActionResult View(string username) {
       // ...
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文