与MVC 3中的路由配合使用的URL路径 - c#

我应该如何格式化路径,使其与jQuery中的MVC路由一起使用?我要返回的Json与另一个SO post非常相似。一切都在本地运行良好,但部署它只会炸毁。该查询运行,但是检查萤火虫时,它似乎不会触发jQuery.Get()的Success回调。

最近更新
我现在唯一的问题是特殊字符。每当我通过“。”或“ @”作为MVC路由的一部分,我得到404。删除这些特殊字符也可以消除错误。您可以在下面看到控制器和路由逻辑。

每当我通过“。”作为网址的一部分,它会爆炸。期间如何?

查询的形式
/服务/索引/ {电子邮件}-

破碎的E.G. /Service/Index/[email protected](404)
工作中/ 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错误。该网址看起来正确。传递字符串值时会出现404错误,但是如果将参数更改为int,则会返回“ null”(带引号)。这使我相信Controller实现或路由出现问题:

    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);
    }

Global.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
            );

参考方案

永远不要硬编码这样的网址:

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) {
        // ...
    });
});

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…

Mongo汇总 - javascript

我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…