Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有附加一致参数的ASP.NET MVC 2路由(不仅仅是控制器和操作)_C#_Asp.net Mvc_Asp.net Mvc 2_Routing - Fatal编程技术网

C# 具有附加一致参数的ASP.NET MVC 2路由(不仅仅是控制器和操作)

C# 具有附加一致参数的ASP.NET MVC 2路由(不仅仅是控制器和操作),c#,asp.net-mvc,asp.net-mvc-2,routing,C#,Asp.net Mvc,Asp.net Mvc 2,Routing,目前,我有如下URL: http://www.example.com/user/create http://www.example.com/user/edit/1 http://www.example.com/org-name/user/create http://www.example.com/org-name/user/edit/1 但现在,我必须支持多个组织及其用户。我需要这样的东西: http://www.example.com/user/create http://www.exam

目前,我有如下URL:

http://www.example.com/user/create
http://www.example.com/user/edit/1
http://www.example.com/org-name/user/create
http://www.example.com/org-name/user/edit/1
但现在,我必须支持多个组织及其用户。我需要这样的东西:

http://www.example.com/user/create
http://www.example.com/user/edit/1
http://www.example.com/org-name/user/create
http://www.example.com/org-name/user/edit/1
我很难让路由正常工作,因此我必须在组织名称的开头添加一个标记,这样路由就不会将其与控制器/操作对混淆。没什么大不了的,但我的URL现在看起来像这样:

http://www.example.com/o/org-name/user/create
http://www.example.com/o/org-name/user/edit/1
那很好。我可以接受

这就是我遇到麻烦的地方:
当我选择一个组织后生成URL时,它不会持久化组织名称。所以当我在这里的时候:

http://www.example.com/o/org-name
…我使用Url.Action(“User”,“Create”)生成Url,它输出:

/user/create
…而不是我想要的:

/o/org-name/user/create
这就是我的路线(顺序):

这和这个问题很相似

我有一种感觉,这将是显而易见的,但现在是星期五,现在还没有发生


编辑:
Womp的建议奏效了,但这是实现自动化的最佳方式吗

public static string ActionPrepend(this UrlHelper helper, string actionName, string controllerName)
    {
        string currentUrl = helper.RequestContext.RouteData.Values["url"] as string;
        string actionUrl = string.Empty;

        if (currentUrl != null)
        {
            Uri url = new Uri(currentUrl);

            if (url.Segments.Length > 2 && url.Segments[1] == "o/")
                actionUrl = string.Format("{0}{1}{2}{3}", url.Segments[0], url.Segments[1], url.Segments[2],
                    helper.Action(actionName, controllerName));
        }

        if(string.IsNullOrEmpty(actionUrl))
            actionUrl = helper.Action(actionName, controllerName);

        return actionUrl;
    }

编辑:
修正了我的工作路线,而不是把它拼凑在一起。最终的解决方案不需要URL中愚蠢的{token}。也许这会帮助其他人:

routes.MapRouteLowercase(
    "Organization",
    "{organization}/{controller}/{action}/{id}",
    new { controller = "Organization", action = "Dashboard", id = UrlParameter.Optional },
    new { organization = @"^(?!User|Account|Report).*$" }
);

routes.MapRouteLowercase(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Core", action = "Dashboard", id = UrlParameter.Optional }
);

Action通过查询虚拟路径提供程序并尝试匹配最特定的路由,使用路由值生成实际的Url。在您使用的表单中,您为控制器和动作提供了值,这与大多数简单网站一样深入,因此是该方法的方便形式。当Url.Action查询路由系统时,它只有一个“控制器”和一个“操作”段要匹配

如果您为该方法提供它所需的其余路由信息,它将正确匹配您想要的路由,并返回正确的URL。试试这个:

Url.Action("User", "Create", new { token = "o", organization = "organization" })

啊哈,成功了!因此,要使其自动化,在UrlHelper上使用扩展方法是最好的吗?我用有效的代码编辑了上面的问题。请看我的评论:将组织存储在会话中而不是路由中是否更有意义?您将如何验证您的用户不尝试访问其他组织?如果您问我,我首先不喜欢您的令牌解决方案。组织名称是一个参数,就像用户的ID一样。您正在编辑ID为1的John Doe,来自组织Contoso。我只需在路由的结尾或中间添加这样的组织参数:{控制器}/{Actudi}/{Orth}}/{ID}。实际上,我会定义更具体的路由,例如create User/create/{organization}或edit User/edit/{organization}/{id}。只有当您使用OrgID和UserID作为复合主用户时,才需要在中使用organizationkey@Ryan我们在Azure上运行,因此我必须将所有会话信息保存在cookie中(这并不可怕)。我们的API已经在每次调用时强制执行安全性,因此猜测其他组织名称(或在不同帐户中有重复名称)的人都会被处理。@mare你是对的,token是一个愚蠢的想法:)我设法修复了路由,以实现我试图实现的目标!我将用我的最终解决方案更新原始问题中的代码。