【问题标题】:URL Paths that work with the routing in MVC 3与 MVC 3 中的路由一起使用的 URL 路径
【发布时间】: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


【解决方案1】:

从不这样的硬编码网址:

var url = '/Service/Index/' + email.toString();

在处理 URL 时始终使用 URL 助手:

$email.blur(function () {
    var email = $(this).val(); // grab the value in the input
    var url = '@Url.Action("Index", "Service")';
    $.get(url, { id: email.toString() }, function(result) {
        // ...
    });
});

无论您的应用程序部署在何处,Url 助手都会始终生成正确的 url。

如果这是一个单独的 javascript 文件,您不能在其中使用服务器端代码,您可以始终在输入字段上使用 HTML5 data-* 属性:

@Html.TextBoxFor(x => x.Email, new { data_url = Url.Action("Index", "Service") })

然后在您单独的 javascript 文件中:

$email.blur(function () {
    var email = $(this).val(); // grab the value in the input
    var url = $(this).data('url');
    $.get(url, { id: email.toString() }, function(result) {
        // ...
    });
});

【讨论】:

  • 这是否适用于 WebForms 中的路由?
  • 你回答了我的第一个问题。创建一个新的。
  • 您只能在使用 razor 文件时使用 URL/HTML Helpers,如果这是 javascript 文件,您将没有该选项。
猜你喜欢
  • 2011-08-30
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 2012-07-06
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多