Jea*_*amp 9 url redirect routing url-encoding asp.net-mvc-3
我想知道如何使用RedirectToAction()传递字符串参数.
假设我有这条路线:
routes.MapRoute(
"MyRoute",
"SomeController/SomeAction/{id}/{MyString}",
new { controller = "SomeController", action = "SomeAction", id = 0, MyString = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
在SomeController中,我有一个动作做一个重定向,如下所示:
return RedirectToAction( "SomeAction", new { id = 23, MyString = someString } );
Run Code Online (Sandbox Code Playgroud)
我尝试使用someString ="!@#$%?&*1"重定向,并且无论我是否对字符串进行编码,它总是会失败.我尝试用HttpUtility.UrlEncode(someString),HttpUtility.UrlPathEncode(someString)和Uri.EscapeUriString(someString)编码它无济于事.
所以我使用TempData来传递someString,但是,我仍然很想知道如何让代码在上面工作,只是为了满足我的好奇心.
小智 3
我认为问题可能出在您的路线顺序中,或者出在您的控制器中。这是我需要工作的一些代码。
路线定义
routes.MapRoute(
"TestRoute",
"Home/Testing/{id}/{MyString}",
new { controller = "Home", action = "Testing", id = 0, MyString = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// note how the TestRoute comes before the Default route
Run Code Online (Sandbox Code Playgroud)
控制器动作方法
public ActionResult MoreTesting()
{
return RedirectToAction("Testing", new { id = 23, MyString = "Hello" });
}
public string Testing(int id, string MyString)
{
return id.ToString() + MyString;
}
Run Code Online (Sandbox Code Playgroud)
当我浏览到时,/Home/MoreTesting我得到了所需的“23Hello”输出,以在浏览器中输出。您可以发布您的路线和控制器代码吗?
| 归档时间: |
|
| 查看次数: |
18524 次 |
| 最近记录: |