用正斜杠"/"分隔的ASP.NET MVC 4参数.没有正确传递参数 [英] ASP.NET MVC 4 Parameters Separated by Forward Slashes "/" not passing args properly

查看:158
本文介绍了用正斜杠"/"分隔的ASP.NET MVC 4参数.没有正确传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遵循许多站点使用的约定,这些站点传递带有多个正斜杠的参数,而不是使用GET模型.

I am trying to follow a convention used by many sites that pass in arguments with multiple forward slashes, as opposed to using the GET model.

也就是说,我希望使用类似以下的网址:

That is, I am looking to consume a URL like:

http://www.foo.bar/controller/action?arg1=a&arg2=b&arg3=c

以这种方式:

http://www.foo.bar/controller/action/a/b/c

我目前使用以下方法(主要)进行此操作:

I currently have this (mostly) working, using the following:

public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Sandbox",
            url: "Sandbox/{action}/{*args}",
            defaults: new { controller = "Sandbox", action = "Index", args = UrlParameter.Optional }

        );
    }

但是,如果我输入类似的内容

However, if I pass in something like

http://www.foo.bar/Sandbox/Index/a 

http://www.foo.bar/Sandbox/Index/a/

Controller和action分别称为:

The Controller and action are appropriately called:

public ActionResult Index(string args)
{
    return View();
}

但是args为空.

但是,如果我输入类似内容:

However, if I pass in something like:

http://www.foo.bar.com/Sandbox/Index/a/b

然后根据需要将args设置为"a/b".

Then args is "a/b", as desired.

我一直在搜索SO和网络的其余部分,但似乎找不到解决方案.

I have been scouring SO and the rest of the web, but can't seem to find the solution.

是否存在明显的我缺少纠正此行为的东西?

Is there something obvious I am missing to correct this behavior?

我要查找错误的术语吗?

Am I looking for the wrong terminology?

注意:我能够使用Windows身份验证使用全新的ASP.NET应用程序来解决此问题.所有这些都完成了:

Note: I was able to repro this issue with a brand new ASP.NET application using Windows Authentication. All that was done:

  1. 在VS 2015中创建ASP.NET应用程序
  2. 选择MVC
  3. 点击更改身份验证
  4. 选择Windows身份验证
  5. 将上述映射路线添加到RouteConfig.cs
  6. 创建SandboxController.cs并将args参数添加到Index
  7. 创建Index.cshtml视图
  8. 使用 http://localhost:55383/Sandbox/Index/a 再现问题
  9. 使用 http://localhost:55383/Sandbox/Index/a/b
  1. Create ASP.NET application in VS 2015
  2. Choose MVC
  3. Click on Change Authentication
  4. Select Windows Authentication
  5. Add the above Map Route to RouteConfig.cs
  6. Create SandboxController.cs and add the args parameter to Index
  7. Create the Index.cshtml view
  8. Repro the problem using http://localhost:55383/Sandbox/Index/a
  9. Repro the expected behavior using http://localhost:55383/Sandbox/Index/a/b

非常感谢您的帮助.谢谢! 类似的问题,但对我没有帮助:在参数中带有斜杠的URL?

Any help is very appreciated. Thank you! Similar question, but doesn't help me: URLs with slash in parameter?

推荐答案

没关系...这是问题所在...

Never mind... Here is the problem...

首先,MapRoute调用默认路由. 要解决此问题,我只是将默认"地图路线替换为沙箱"路线.

The MapRoute is calling the default route, first. To fix it, I just swapped the Default map route with the Sandbox route.

我希望这对某人有帮助.

I hope this helps someone.

工作解决方案:

public class RouteConfig {
    public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Sandbox",
            url: "Sandbox/{action}/{*args}",
            defaults: new { controller = "Sandbox", action = "Index", args = UrlParameter.Optional }

        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );


    }
}

这篇关于用正斜杠"/"分隔的ASP.NET MVC 4参数.没有正确传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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