How to use App Service methods as REST API endpoints? - c#

POST /api/services/app/Company/Create is the URL we normally get when we create App Service in ASP.NET Boilerplate.
How can I achieve POST /api/services/app/Company using App Service? Because if I use OData, then some response format is not in ABP response format.
Here, POST is the HTTP method and Create is the name of the method in CompanyAppService of my ABP Application project. What I want is when I consume this API on any web app, then I should be able to call this API at /api/services/app/Company with HTTP method POST. I don't want to use Create, Update, Delete, Get, GetAll in URL that we are going to use while calling in web application.

You can achieve this by following way.
[HttpPost("api/services/app/Company")]
public async Task CreateCompany(CompanyDetailsDto input)
[HttpDelete("api/services/app/Company")]
public async Task DeleteCompany(EntityDto input)
[HttpGet("api/services/app/Company")]
public async Task GetCompany(EntityDto input)
[HttpPut("api/services/app/Company")]
public async Task UpdateCompany(CompanyDetailsDto input)

Related

Accept multipart/form-data request in .Net Core

You need to send a request to the .Net Core application using Soap Ui. The request contains some data in the json format + an attached file.
I can’t find it anywhere and I can’t get either the data or the content of the file. Still as an option, what am I sending wrong via Soap? How to get everything you need through HttpContext.Request.Body or something like that?
[Route("betrsign"), AllowAnonymous, ApiController]
public sealed class ServiceController : Controller
and it has a post method
[HttpPost("service"), DisableRequestSizeLimit]
[DisableFormValueModelBinding]
public async Task<ActionResult> Service()

call a regular MVC controller method from a webapi2 service

Given an MVC controller method, that builds and returns a PDF.
[HttpGet]
[Route("pdf/{acc}/{sign}")]
public async Task<ActionResult> Download(string acc, string sign)
{
... // omitted some irrelevant detail.
var html = this.View("Letter", model).Capture(this.ControllerContext);
byte[] pdf = this.CreatePdfFromHtml(html);
return this.File(binary, "application/pdf", file);
}
The Capture extension method captures the html output, which is then returned as a file.
I need to execute the above method (or something close) in the below webapi2 method.
I need the credentials and other login state to be present. The webapi and MVC requests are within the same application that uses cookies for security.
[HttpPost]
[Route("generate")]
public int Generate(MyRequestModel request)
{
byte[] pdf = ... // how do i get the file above??
}
You should either request the MVC action via HttpClient or simply factor out the PDF creation into a helper class that both your MVC and Web Api actions can reference.
With the first method, you would need to some how authenticate the request, which is going to be a bit difficult to do with an MVC action. MVC uses a multi-step authorization: sign in, verify, set cookie, redirect. You would have to follow those same steps in order to get a cookie set via HttpClient. Think of it as a little mini-browser. Web Api, on the other hand, simply accepts an Authorization header, since API requests are idempotent.
The easiest and most straight-forward route, especially since both your MVC and Web Api reside in the same application, would be to simply factor out the PDF creation code into a helper class. Your MVC and Web Api actions, then, can simply just call some method on that class to get the PDF, reducing code duplication.

Invoke method/Do action before every POST/GET request .NET Core

What I am trying to achieve - My application is simply ASP .Net Core application. It is not Web API. I want to execute method before every post/get request from my app to external sources, for example:
I am sending a post request, to check SSL expiry date to some website API and it returns me a response. According to the response I am sending another request or not. I don't want to place call method statement before every request, I would like to do it globally.
I was trying to achieve this based on http://www.sulhome.com/blog/10/log-asp-net-core-request-and-response-using-middleware
As it occurs, this middleware works(I have it working) only for internal requests(routing requests inside application).
Is there any possibility to do it for all requests?
Thanks in advance
.NET Core allows to create custom middlewares to get into MV pipeline. Here is an example:
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
//do your checkings
await _next(context);
}
}
In Startup.cs in Config method just regiester it:
app.UseMiddleware<MyMiddleware>(Options.Create(options));
Since you are talking about doing a call on Outgoing requests, you have two mechanisms to solve the problem:
Use an Aspect Oriented Programming Library (like https://www.postsharp.net)
Implement your own Request class (that has the global behavior you desire) and make sure that all requests are done using this class (or a class that inherits from it).
For the second point, a good mechanism is that the base class provides a SendRequest method that receives an HttpRequestMessage and executes the global code. Classes that inherit from it use this method to send the requests and have no access to the underlying HttpClient (so that they cannot run around the SendRequest method).

How to implement SOAP service on WebAPI

We have a server which has several types of api (custom XML API based on httplistener, SOAP API based on WCF and REST API based on WEB API). We want to move all API's to WEB API (there are many reasons) and it should be backward compatible.
One of the reason to support url structure: services/service1. services/service2. And in this case it should be on one port. It is intranet application which is distributed to multiple customers and it should be easy to deploy, install. So, we can not have a long configuration on customer side (proxing and otherts).
Are there easy way for implementation SOAP service on web api? At first look should be easy way to parse httprequest to typed soap envelope (based on existed contract) and serialize a answer. Of course, there many actions and data types in contract.
PS: I do not want to look into servicestack:)
Update:
The problem I described above can be fixed by proxing http request to soap service (It can work only with basichttpbinding without security. If WCF service require NTLM authentication it won't work):
[HttpPost]
public async Task<IHttpActionResult> SoapAction()
{
var httpClient = new HttpClient();
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost:8111/soap")
{
Content = this.Request.Content
};
foreach (var header in this.Request.Headers)
{
httpRequestMessage.Headers.Add(header.Key, header.Value);
}
var responseMessage= await httpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
return ResponseMessage(responseMessage);
}
But I still want to know are there any SOAP parser in C# because my server supports NTLM authentication.
I wouldn't recommend to mix the technologies. Have one project for SOAP Apis and another one for the WebApi, sharing the same logic.
You have then one url for soap, the other one to webapi.
Edit:
I wouldn't do the SOAP Parser at all. That was the power of WCF and would keep on using it.
Since proxing is not an option (Which could be done in web.config and easily deployed), I would create a WebAPI endpoint which would redirect to SOAP API.
[HttpGet]
public IHttpActionResult Service1()
{
return Redirect("http://service.com/soap/services/service1");
}
Later, when migrating the logic, use the service itself.
[HttpGet]
public IHttpActionResult Service1()
{
var result = new ServiceLogin1().Execute();
if(result == null)
{
return StatusCode(HttpStatusCode.NoContent);
}
else
{
return Ok();
}
}
I think this is already answered here ASP.NET WebAPI + Soap
If what you are asking for is how to create REST wrappers that call into the SOAP implementations then library's like ServiceStack do this for you but if you want to do it yourself with WebApi it's pretty easy. Just make a separate project that has your SOAP service references in it wrapped in some sort of abstraction and then reference that in your WebApi project and call into it from your REST endpoints.
If what you are asking is how to host the SOAP interfaces in WebApi I think you are just making more work for yourself. Use the WCF scaffolding that MS has provided. WebApi for REST services, WCF for SOAP.

web api asynchronous method from .net application

I have a web api 2 application and a wpf client. my question is that LINQ to Entities support multiple asynchronous method, like ToListAsync, ToArrayAsync and so on.
But in my wpf client application, there are no such things.
Can anyone give me some examples for asynchronous method in client application?
Web API calls are just straight HTTP calls. Just use the HttpClient class.
Asynchronous coding reference
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();
string urlContents = await getStringTask;

Categories