1

How should I format a path so it works with MVC routing in jQuery? I'm returning Json, very similar to another SO post. Everything works great locally, but deployed it just bombs out. The query runs, but checking firebug it looks like the Success callback for jQuery.Get() doesn't fire.

LATEST UPDATE
The only issue I have now is with special characters. Anytime I pass a "." or "@" as part of the MVC route I get a 404. Removing these special characters also removes the error. You can see the Controller and Routing logic below.

Anytime I pass a '.' as part of the URL it bombs. Whats up with the periods?

Queries are of the form /Service/Index/{email} -

Broken E.G. /Service/Index/[email protected] (404)
Working E.G. /Service/Index/bmackeyfoocom (200)

Old stuff (for reference)

I tried "/Service/Index/email", "../Service/Index/email","../../Service/Index/email", but nothing is working.

    $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(), 

Update
I stopped hardcoding my URL. Local runs work perfectly. I still get a 404 error when I run on DEV server. The URL looks correct. I get a 404 error when I pass a string value, but if I change my parameter to an int I get a returned "null" (with quotes). This leads me to believe something is wrong with my Controller implementation or routing:

    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
            );
3
  • use the firebug network tab to see what url is being called, and what the server response is. That should give you a clue. Commented Mar 18, 2011 at 21:59
  • Can I ask what you are trying to do? Is this for validation? Commented Mar 18, 2011 at 22:15
  • @Paul this is to populate a bunch of employee information given their email address. Commented Mar 19, 2011 at 1:44

1 Answer 1

3

Never hardocde urls like this:

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

Always use URL helpers when dealing with URLs:

$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 helpers will always generate correct urls no matter where your application is deployed.

And if this is a separate javascript file in which you cannot use server side code you could always use HTML5 data-* attributes on your input field:

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

and then in your separate javascript file:

$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) {
        // ...
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

does this apply to routing in WebForms?
You answered my first question. Creating a new one.
You can only use URL/HTML Helpers if you are on a razor file, if this is a javascript file you won't have that option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.