Reputation: 815
I built an ApiController application. My tests are running fine, the only problem is that I can't make it wait for a request. I want to run the application in Visual Studio and try an external AJAX request. However, when I run the application in Visual Studio, it run the whole application.
Here is my controller:
public class PostController : ApiController
{
[HttpPost]
[Route("api/test")]
public string Post([FromBody] Data m)
{
PostingToCL post = new PostingToCL();
string postReturn = post.Post();
return string.Format(postReturn);
}
}
Now, I want to make it wait I run an external html file that will call this API. Does it make sense? How can I do it?
Upvotes: 0
Views: 470
Reputation: 146
Check you route. I like to put route on the whole controller:
[Route("api/[controller]")]
public class MyController: Controller{
[HttpPost]
public string Post([FromBody] Data m)
{
PostingToCL post = new PostingToCL();
string postReturn = post.Post();
return string.Format(postReturn);
}
}
Give me a more specific example and use case. I'm thinking this is what you you are looking for: https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api
I highly suggest to look at an example application: https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
Download the source code and run it: https://code.msdn.microsoft.com/Sample-code-of-Getting-c56ccb28
Then debug the application on the back end: Your controllers would be under Controllers folder. See the project structure and data flow. Let me know if you have questions.
Upvotes: 1