【发布时间】:2011-03-18 21:55:17
【问题描述】:
我应该如何格式化路径以便它与 jQuery 中的 MVC 路由一起使用?我要返回 Json,与另一个 SO post 非常相似。一切都在本地运行良好,但部署它只是炸弹。查询运行,但检查 firebug 看起来 jQuery.Get() 的 Success 回调没有触发。
最新更新我现在唯一的问题是特殊字符。每当我通过一个“。”或“@”作为 MVC 路由的一部分,我得到 404。删除这些特殊字符也会删除错误。您可以在下面看到控制器和路由逻辑。
每当我传递一个“。”作为它轰炸的 URL 的一部分。 句点是怎么回事?
查询的形式为 /Service/Index/{email} -
破碎的 E.G. /服务/索引/bmackey@foo.com (404)
工作 E.G. /Service/Index/bmackeyfoocom (200)
旧东西(供参考)
我尝试了"/Service/Index/email", "../Service/Index/email","../../Service/Index/email",但没有任何效果。
$email.blur(function ()
{
var email = $(this).val(); // grab the value in the input
var url = '@Url.Action("Index", "Service")'; //this is calling MVC page, not normal aspx so I can't pass it as a query param (at least as far as I am aware)
$.get(url + '/' + email.toString(),
更新
我停止对我的 URL 进行硬编码。本地运行完美。在 DEV 服务器上运行时仍然出现 404 错误。 URL 看起来正确。传递字符串值时出现 404 错误,但如果将参数更改为 int,则会返回“null”(带引号)。这让我相信我的控制器实现或路由有问题:
public ActionResult Index(string email)
{
string emailAddress = email;
GetActiveDirectoryInformation adInfo = new GetActiveDirectoryInformation();//calls entity framework
Common_GetAdInfo_Result result = adInfo.Lookup(email: emailAddress);
string jsonResponse = System.Web.Helpers.Json.Encode(result);
return Json(jsonResponse,JsonRequestBehavior.AllowGet);
}
全球.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Service",
"Service/Index/{email}",
new { controller = "Service", action = "Index", email = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
【问题讨论】:
-
使用 firebug 网络选项卡查看正在调用的 url 以及服务器响应是什么。这应该会给你一个线索。
-
我能问一下你想做什么吗?这是为了验证吗?
-
@Paul 这是要根据他们的电子邮件地址填充一堆员工信息。
标签: c# jquery asp.net-mvc-3