简体   繁体   中英

cannot connect to Web API

I am new to WebAPI programming .Here is what have I done

  1. Created ASP.NET web Application SampleWebApiProject in Visual Studio 2013
    under .NET Framework 4.5.2

  2. Selected MVC and checked Web API under [Add Folders and core references for].

  3. using Nuget package installed knockout.js ,knockout-validation.js etc etc.

In my code for Login.cshtml I have html button

    <div>
        <button type="button" class="btn btn-info" data-bind="click:$parent.login">
          Login
       </button>
   </div>

And on my click button I have

  self.viewModelHelper.apiPost('api/account/login', unmappedModel,
                function (result) {

        }

And I have created API Controller called AccountApiController

 public class AccountApiController : ApiController
 {

    [HttpPost]
    [POST("api/account/login")]
    public HttpResponseMessage Login(HttpRequestMessage request, [FromBody]AccountLoginModel accountModel)
    {
        return null;
    }
}

However when I inspect the click event in Chrome developer tools I get an error response

POST http://localhost:64436/api/account/login 404 (Not Found) .

this is my WebApiConfig

// Web API routes
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
       name: "DefaultApi",
       routeTemplate: "api/{controller}/{id}",
       defaults: new { id = RouteParameter.Optional }
);

Am I working with right type of application ?

Screenshot 屏幕截图

Try:

  self.viewModelHelper.apiPost('api/accountapi/login', unmappedModel,
            function (result) {

    }

and API Controller

public class AccountApiController : ApiController
{

    [HttpPost]
    [POST("api/accountapi/login")]
    public HttpResponseMessage Login(HttpRequestMessage request, [FromBody]AccountLoginModel accountModel)
    {
        return null;
    }
}

Your account controller is named accountapi and not account, so webapi can't find any controller called account.

I'm not sure, but your parameters look wrong in your webapi controller...

Why would you add HttpRequestMessage as a parameter?

您已经调用了控制器AccountApiController ,因此api/account/login应该是accountapi/login

Web API has a strict calls when it comes to MVC architecture.

If you call POST. It means that the API will really CREATE a new Entity, and Does NOT, make other request to be returned.

So meaning, the WebAPI is not custom API function Call that you thought it might be.

It is different from creating an individual API to Creating an web API inside an MVC Application.

Here is are some Notes.

  • GET : Retrieve an entity
  • PUT : update an entity
  • POST : create a new entity
  • DELETE : remove an existing entity.

so let us say you have an API for Account Models. I will say Models cause when creating an Web API. You need a Model. Unless you're creating your custom API. Outside the MVC.

Now you did this. api/account/test What it will do is use the [GET] function.

Whatever function you have in the account controllers that have a Data Annotation of [GET] will be executed. And return you something.

And No. Don't use Login as the name of the Method just use GET as you can't really tell the Web API which function to use. It WILL use the one with the GET data annotation. So entering api/account/ login <---- this will not call the login method, it is entering a string data to be passed to the Get Method.

[HttpGet]

public IEnumerable<string> Get()
{
    return "No Value";
}

[HttpGet("{id}")]

public IEnumerable<string> Get(int id)
{
    return "There is a value";
}

Now if you want the POST to be Called. Simply create a A Form that has a method of POST. Or JQuery Javascript and call generate the POST method for them. You can't write the Method call in the address bar. You just have to use the right kind of request to call the specific function or function with overload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM