如何动态添加到ASP.NET MVC RouteTable? [英] How can I dynamically add to the ASP.NET MVC RouteTable?

查看:76
本文介绍了如何动态添加到ASP.NET MVC RouteTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们网站上有一个区域,人们可以注册并在网站上获得自己的页面,我们希望在〜/ pageSlug 上托管该页面。我尝试使用Global.asax中的一条规则来执行此操作,但这打破了基本的默认路由,该默认路由允许〜/ Controller 直接映射到Index操作。我不允许在userSlug前面放置任何分隔符,因此〜/ p / pageSlug 在这里实际上不是一个选择。

We've got an area on our site where people can sign up and be given their own page on the site which we want to host at ~/pageSlug. I've tried doing it with a rule in Global.asax, but that broke the fundamental default route that allows ~/Controller to map directly to the Index action. I'm not being allowed to put any kind of separator in front of the userSlug, so ~/p/pageSlug isn't really an option here.

在将用户页面添加到路由方面,我循环浏览App_Start中的页面并将其显式添加到RoutesTable中。一切正常,并且我们将AppPool刷新的时间设置得足够长,以使其成为每天一次的任务。不过,这确实使我们有24小时的周转时间为我们的用户使页面实时显示,这是我要解决的问题。

In terms of getting the user pages added to the routes, I'm cycling through the pages at App_Start and explicitly adding them to the RoutesTable. This is working fine, and we've got AppPool refreshes set long enough to make this a once a day task. This does leave us with a 24-hour turnaround to "get pages live" for our users though, which I'm trying to solve.

理想情况下,这样做是在用户注册后将相关路由动态添加到RouteTable。我尝试使用以下方法做到这一点:

Ideally, what I'd like to do is add the relevant route to the RouteTable dynamically once a user has signed up. I've tried doing that with:

RouteTable.Routes.Add(
    RouteTable.Routes.MapRoute(
        toAdd.UrlSlug + "Homepage", 
        toAdd.UrlSlug, 
        new { controller = "Controller", View = "View", urlSlug = toAdd.UrlSlug }
    )
);

但这似乎没有用。我在其他任何地方都找不到解决方案,而且我确信我的代码既太天真,又背叛了对路由的理解-请帮忙!

but that didn't seem to work. I can't find a solution anywhere else, and I'm sure my code is both horribly naive and betrays a lack of understanding of routing - please help!

推荐答案

使用路由约束尝试该操作怎么办。找出所有用户列表并约束所选路由以匹配该列表中的条目

What if you try this, using route constraint. Get out list of all users and constraint the chosen route to match entries in that list

public class UserPageConstraint : IRouteConstraint
{
    public static IList<string> UserPageNames = (Container.ResolveShared<IUserService>()).GetUserPageNames();

    bool _IsUserPage;
    public UserPageConstraint(bool IsUserPage)
    {
        _IsUserPage = IsUserPage;            
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (_IsUserPage)
            return UserPageNames.Contains(values[parameterName].ToString().ToLower());
        else
            return !UserPageNames.Contains(values[parameterName].ToString().ToLower());
    }
}

然后在Global.asax.cs中进行设置用户的路由,如下所示:

Then in the Global.asax.cs, set up a route for users as follows:

routes.MapRoute("UserHome", "{userPage}", new { controller = "UserPageController", action = "Index" }, new { userPage = new UserPageConstraint(true) });

对于上述路由,在UserPageController的动作 index中,将userPage作为

For this above route, in the action 'index' of the UserPageController, we will have the userPage as the parameter.

对于与userPage Home相关的其他路由,我们可以相应地添加路由。例如,可以按以下方式添加userdetails页面路线:

For other routes relative to the userPage Home, we can add routes accordingly. for example, the userdetails page route can be added as follows:

routes.MapRoute("UserHome", "{userPage}/mydetails", new { controller = "UserPageController", action = "Details" }, new { userPage = new UserPageConstraint(true) });

您可以尝试看看。

这篇关于如何动态添加到ASP.NET MVC RouteTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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