4

I have a controller ValidationController and it has a method called EmailExist

I have an ajax call

$.ajax({    
  type: "POST",    
  url: "/Validation/EmailExist",    
  data: { 'Email': $('#Email').val() },    
  success: function (result) {    
    if (result == true) {    
      alert("true");  
    } else {   
      alert("false");  
    }    
  },  
  error: function (error) {  
    alert(error.responseText);  
  }  

I get a 404 error and I have no clue why? Any suggestions or suggestions on how to debug this?

Sampson
  • 265,109
  • 74
  • 539
  • 565
Pittfall
  • 2,751
  • 6
  • 32
  • 61

1 Answers1

3

You have hardcoded an url:

url: "/Validation/EmailExist"

That's very bad. You should always use url helpers when dealing with urls:

url: "@Url.Action("EmailExist", "Validation")"

The reason why this is bad is because since you have hardcoded the url you no longer rely on your route configuration. If you change your routes in Global.asax your code will break. Your code will also break when you deploy your application in IIS because now there's a virtual directory name that you should take into account. So the correct url is no longer /Validation/EmailExist but /MyAppName/Validation/EmailExist. All those things are taken into account by the helpers and that's the reason why you should always use them.

Of course if this is in a separate javascript file in which you don't have access to server side code you could use HTML5 data-* attributes inside your DOM to put the correct url or directly use some existing element such as a form action or anchor href.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    That was a good tip but it was not my issue – Pittfall May 15 '12 at 15:26
  • Did you change your routes setup? Are you sure that there's a public class called `ValidationController` deriving from `Controller` and possessing a public action called `EmailExist`? – Darin Dimitrov May 15 '12 at 15:28
  • I had another MVC project in the same solution that was colliding with my current project. So in the Global.asax, I added namespaces to my routes.MapRoute because Home Controller was in 2 websites on IIS, I guess, and it didn't know which one to use. Since then, I can't call my controller method – Pittfall May 15 '12 at 15:35
  • Another option for storing Urls for JS is to do something like App.Urls.EmailExistUrl = @Url.Action... – Shane Courtrille May 15 '12 at 18:24
  • btw Darin, razor does not work in a jquery.... – Pittfall May 16 '12 at 13:01
  • You mean in separate javascript files? Of course that it doesn't work. They are static. But there are so many other possible ways to define this url somewhere in your DOM and then simply access it in your separate javascript file. – Darin Dimitrov May 16 '12 at 13:14
  • Ok but there has to be some sort of shortcut.... I don't want to specify that on every page. There has to be a way to get the virtual directory with special symbol or something. – Pittfall May 16 '12 at 16:55