如何在路线ASP.NET MVC传统类型的网址 [英] How to route legacy type urls in ASP.NET MVC

查看:119
本文介绍了如何在路线ASP.NET MVC传统类型的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我无法控制的因素,我要处理这样的URL:

Due to factors outside my control, I need to handle urls like this:

  • http://www.bob.com/dosomething.asp?val=42

我想他们的路线与VAL特定的控制器/动作已经被解析和绑定(即一个参数的动作)。

I would like to route them to a specific controller/action with the val already parsed and bound (i.e. an argument to the action).

我的理想动作是这样的:

Ideally my action would look like this:

ActionResult BackwardCompatibleAction(int val)

我发现这个问题:<一href=\"http://stackoverflow.com/questions/817325/asp-net-mvc-routing-legacy-urls-passing-querystring-ids-to-controller-actions\">ASP.Net MVC路由传统的URL查询字符串传递到IDS控制器行动,但重定向是不能接受的。

I found this question: ASP.Net MVC routing legacy URLs passing querystring Ids to controller actions but the redirects are not acceptable.

我已尝试解析带问号的查询字符串部分,但任何路由无效的路由。

I have tried routes that parse the query string portion but any route with a question mark is invalid.

我已经能够路由这个请求:

I have been able to route the request with this:

routes.MapRoute(
    "dosomething.asp Backward compatibility",
    "{dosomething}.asp",
    new { controller = "MyController", action = "BackwardCompatibleAction"}
);

不过,从那里,只有这样才能得到val值=?可通过的Request.QueryString。虽然我可以解析控制器内的查询字符串它将使测试行动更加困难,我会preFER不要有依赖关系。

However, from there the only way to get to the value of val=? is via Request.QueryString. While I could parse the query string inside the controller it would make testing the action more difficult and I would prefer not to have that dependency.

我觉得好像有什么东西我可以与路由做的,但我不知道它是什么。任何帮助将是非常美联社preciated。

I feel like there is something I can do with the routing, but I don't know what it is. Any help would be very appreciated.

推荐答案

参数 VAL 你的 BackwardCompatibleAction 方法中应该与查询字符串值被自动填充。路线并不意味着处理查询字符串。你在你的问题中列出的解决方案看起来我的权利。您是否尝试过它,看看会发生什么?

The parameter val within your BackwardCompatibleAction method should be automatically populated with the query string value. Routes are not meant to deal with query strings. The solution you listed in your question looks right to me. Have you tried it to see what happens?

这也将工作路线。既然你同时指定控制器和动作,你不需要花括号参数。

This would also work for your route. Since you are specifying both the controller and the action, you don't need the curly brace parameter.

routes.MapRoute(
    "dosomething.asp Backward compatibility",
    "dosomething.asp",
    new { controller = "MyController", action = "BackwardCompatibleAction"}
);

如果您需要参数化操作名称,那么这样的事情应该工作:

If you need to parametrize the action name, then something like this should work:

routes.MapRoute(
    "dosomething.asp Backward compatibility",
    "{action}.asp",
    new { controller = "MyController" }
);

这会给你一个更通用的路由可以匹配多种不同的.asp网页网址为行动的方法。

That would give you a more generic route that could match multiple different .asp page urls into Action methods.

http://www.bob.com/dosomething.asp?val=42
将路线 MyController.dosomething(INT VAL)

http://www.bob.com/dosomethingelse.asp?val=42
将路线 MyController.dosomethingelse(INT VAL)

这篇关于如何在路线ASP.NET MVC传统类型的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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