1

I am somewhat new to ASP. I want to make an ajax call, but I cannot figure our the URL to send to. What on earth is the actual URL to the application? Of course it comes up as http:// localhost : someport/ (spaces added because the link violates SO questions) in the browser. When I try typing in ["index","default"].["asp","aspx","htm","html","asm","asmx"] afterwards I get an error.

The relevant JavaScript portion looks like this:

$.ajax({
   "url": "Default.aspx/ProcessReq",

And the relevant C# code in my controller looks like:

[WebMethod]
public object ProcessReq(string s) {

I have added the following to the config file:

<configuration>
   <system.web>
      <webServices>
        <protocols>
           <add name="HttpGet"/>
           <add name="HttpPost"/>
         </protocols>
      </webServices>
   </system.web>
</configuration>

I'm sure I am doing something completely wrong, and I'll stress once again that ASP is fairly new to me. Any ideas?

Paul
  • 5,700
  • 5
  • 43
  • 67
  • paul, MVC links depend on routes. But assuming that you are using a Default route, your URL will be **/Controller/Action/**. Your Web method would be replaced by a Controller class with an ActionResult Method. – Dave Alperovich Mar 18 '13 at 00:27
  • see this for reference : http://stackoverflow.com/q/7770679/235710 – Adil Shaikh Mar 18 '13 at 00:34
  • Default.aspx/ProcessReq would typically work only if you were using PageMethods, where by your code is directly in code-behind file - default.aspx.cs - and identified as a webmethod, typically though... @DaveA is correct – kpcrash Mar 18 '13 at 00:41
  • @DaveA thanks for taking the time to look at it. I changed the URL in the browser to /home/index/default.aspx. It loaded my page. I then changed my url in the ajax call to "/home/index/default.aspx/ProcessReq". It still fails with a 404 error. I'll add that I'm not trying to return a new view. The ajax request simply handles a database insert, and returns a status of that result. The success function of the ajax call updates the page as needed. – Paul Mar 18 '13 at 00:46
  • Seems like you are coming to MVC with Web Forms still fresh in your mind. That's common. Unfortunately MVC is very different than WebForms. You would NOT be calling Views (aspx or razor) directly. You call you Action Method: /home/index ... and the default.aspx can be left out. try it. – Dave Alperovich Mar 18 '13 at 00:49
  • @DaveA Thank you. I no longer get the 404 error. I don't get any data back either, but I think you already answered this problem when you said that I would need to make a new controller class with an ActionResult method. Currently the entire page HTML comes back in the success() function. If you post your last comment as an answer, I can accept it. – Paul Mar 18 '13 at 01:06

1 Answers1

1

Seems like you are coming to MVC with Web Forms still fresh in your mind. That's common.

Unfortunately MVC is very different than WebForms. You would NOT be calling Views (aspx or razor) directly. You call you Action Method: /home/index ... and the default.aspx can be left out. try it.

ActionResult is base class of response types returned by Controller Actions

public ActionResult ProcessReq()
{
    return View(model)
}

or your ProcessReq can be of Json type

public ActionResult ProcessReq()
{
    Json(new { page= "<HTML></HTML>", control= "<input type='text' id='control'>" });
}

Action Results can return Views', PartialView's and JSon (among many other possibilities). In this case you have to decide which of these response types you want, depending on the implementation requirements of your design.

Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101