我怎么路线在ASP.NET MVC查询字符串的URL? [英] How do I route a URL with a querystring in ASP.NET MVC?

查看:130
本文介绍了我怎么路线在ASP.NET MVC查询字符串的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在设置一个MVC自定义路由采取URL从另一个系统中的格式如下:

../ ABC / ABC01键= 123安培;组= 456

第二ABC后的01步数这将改变和密钥和参数的集团将改变。我需要的路线这一个动作与步骤号键和组作为paramters控制器。我已经尝试以下code然而,它抛出一个异常:

code:

  routes.MapRoute(
    OpenCase
    ABC / ABC {} stepNo键为{}键放&;组= {}组,
    新{控制器=ABC1,行动=OpenCase}
);

例外:

 `路由URL不能以'/'或'〜'字符开始,它不能包含? character.`


解决方案

您不能包含在路线查询字符串。

:像这样的路线尝试

  routes.MapRoute(OpenCase,美国广播公司/ ABC {} stepNo
   新{控制器=ABC1,行动=OpenCase});

然后,控制器上添加一个方法是这样的:

 公共类ABC1:控制器
{
    公众的ActionResult OpenCase(字符串stepno,字符串键,弦乐群)
    {
        //在这里做的东西
        返回查看();
    }
}

ASP.NET MVC将自动在控制器中的方法的查询字符串参数的参数映射。

I'm trying to setup a custom route in MVC to take a URL from another system in the following format:

../ABC/ABC01?Key=123&Group=456

The 01 after the second ABC is a step number this will change and the Key and Group parameters will change. I need to route this to one action in a controller with the step number key and group as paramters. I've attempted the following code however it throws an exception:

Code:

routes.MapRoute(
    "OpenCase", 
    "ABC/ABC{stepNo}?Key={key}&Group={group}",
    new {controller = "ABC1", action = "OpenCase"}
);

Exception:

`The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.`

解决方案

You cannot include the query string in the route. Try with a route like this:

routes.MapRoute("OpenCase", "ABC/ABC{stepNo}",
   new { controller = "ABC1", action = "OpenCase" });

Then, on your controller add a method like this:

public class ABC1 : Controller
{
    public ActionResult OpenCase(string stepno, string key, string group)
    {
        // do stuff here
        return View();
    }        
}

ASP.NET MVC will automatically map the query string parameters to the parameters in the method in the controller.

这篇关于我怎么路线在ASP.NET MVC查询字符串的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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