在控制器中,操作被重定向到同一控制器的另一个操作。结果:“HTTP错误404.0 - 未找到”。在JQuery中通过ajax-request调用GetData。在重定向时请求网址http://localhost:61327/Home/Index/qwertyQWERTY%20HTTP/1.1。请求地址http://localhost:61327/Home/Index/qwertyQWERTY运行正常。控制器代码,ajax-request和RouteConfig.cs如下所示。
路线
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{d}",
defaults: new { controller = "Home", action = "Index", d = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default1",
url: "{controller}/{action}/{data}",
defaults: new { controller = "Home", action = "GetData", data = UrlParameter.Optional }
);
}
的HomeController
public ActionResult Index(string d)
{
Repository repository = new Repository();
ViewBag.ViewbagValues = repository.GetAllCustomersAndOrders();
Response.Write("Ku-ku");
Response.Write(d);
return View(repository.GetAllCustomersAndOrders());
}
[HttpPost]
[ValidateInput(false)]
public ActionResult GetData(string data)
{
Response.Write(Request.InputStream);
Response.Write("qwerty");
return RedirectToAction("Index", "Home", new {d="qwertyQWERTY"});
}
脚本
function SendDataToController(data) {
$.ajax({
url: "Home/GetData",
type: "POST",
datatype: "text",
contentType: "application/text; charset=utf-8",
data: data,
success: function (result) {
alert("Data was send to the controller");
window.location = result.URL;
},
error: function (err) {
alert("Error: data was not send to the controller");
}
});
alert(data);
答案 0 :(得分:0)
除了Stephen Muecke的说明 - 这将适用于您的javascript:
[HttpPost]
[ValidateInput(false)]
public ActionResult GetData(string data)
{
var data = new { URL = Url.Action("Index", "Home", new {d="qwertyQWERTY"}) };
return Json(data, JsonRequestBehavior.AllowGet);
}