I've used Laravel to build web apps, but now I'm trying to learn ASP.NET CORE. I need to create a custom middleware and apply it to an action of a controller. How can I do it? Thanks.
In .net core sense of middlewares, middleware runs early - before the route is known. Each middleware is used for each request. For what you want, you can use action filters. Action filters can have services injected into them - and will most likely be sufficient for you. https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-3.1
Here's a link!
Taking the middleware specific approach in .NET Core 3.1, we can conditionally add middleware using the following-
In configure method-
app.UseWhen(context=>context.Request.Path.StartsWithSegments("your-route-url"),branch=>branch.useMiddleware(););
There are a few ways how the pipeline branching can happen, follow the docs for more information-
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0#branch-the-middleware-pipeline
Related
I'm working on migrating an ASP.NET WebAPI application to ASP.NET Core. In this application I'm using batch routes (as described here: https://devblogs.microsoft.com/aspnet/introducing-batch-support-in-web-api-and-web-api-odata/). It's quite elegant - all I need to do is to map a batch route using config.Routes.MapHttpBatchRoute, and then just batch whatever API requests I want to from the client. However, I haven't found an alternative to this in ASP.NET Core. Does it exist, or do I need to write batch APIs myself that perform what I need to do?
I've had success using this library: https://github.com/Tornhoof/HttpBatchHandler
It works for .Net Core 3.0 and newer. The documentation isn't the best though.
During startup of my .NET Core 2.0 web API, I register routes to my controllers via the MapRoute() method. I would like swagger to recognize these routes or allow me to add them during startup. I know the documentation indicates swagger does not support conventional routing out of the box (only attribute based routing), but does anyone have any alternatives to providing documentation for routes created dynamically during startup?
I have created Swashbuckle.AspNetCore.SwaggerGen.ConventionalRouting which replaces the original Swagger generator with a custom one that recognizes conventional routes. It supports ASP.NET Core 2.1 and ASP.NET Core 3.X applications. I have used it in a production application with more than 400 dynamic conventional routes (custom constraints, regexes, etc) and the results are pretty good.
Let's start with an example:
I've added mvc core to asp.net core pipeline two times on different branches. See following sample codes:
aspNetCoreApp.Map("/1", innerAspNetCoreApp => { innerAspNetCore.UseMvc(...); })
aspNetCoreApp.Map("/2", innerAspNetCoreApp => { innerAspNetCore.UseMvc(...); })
And I've following services configuration:
services.AddMvcCore().AddJsonFormatters().AddApplicationPart(assembly1ForExample).AddControllersAsServices();
I'd like to have something like this too:
services.AddMvcCore().AddXmlFormatters().AddApplicationPart(assembly2ForExample).AddControllersAsServices();
I'd like to use first configuration for first branch ('/1'), and the second configuration for second branch ('/2')
Note that this is a sample only, I'm not looking for some extensibility solutions in asp.net core mvc, I'd like to find a solution in asp.net core itself.
Imaging that You've developed an middleware to write exception details to response. (I'm aware of AspNetCore.Diagnostics package, this is a sample only).
Based on branching you can write something like this:
aspNetCoreApp.Map("/1", innerAspNetCoreApp => { innerAspNetCore.UseBeautifulExceptionWritter(...); })
aspNetCoreApp.Map("/2", innerAspNetCoreApp => { innerAspNetCore.UseBeautifulExceptionWritter(...); })
Now let's imaging that we've a interface to make exception details writing extensible. For example IExceptionDetailResolver, and there is a default implementation named DefaultExceptionDetailResolver. In services configuration I'd like to register different implementations for each usages of that middleware.
In Owin pipeline, I was able to achieve this using nested containers. I created a root container for the whole application, and I created nested containers for each web api configurations.
I was able to register different implementations for asp.net web api interfaces every time I added http configuration to pipeline. For example I was able to add web api several times to owin pipeline on different branches, one time for odata, one time for asp.net web api itself normal usages, and one time for web hooks. And I was able to provided Different IAssemblyResolver implementations for each of them.
How can I achieve this in asp.net core?
Additional notes: I've managed to do this for owin pipeline on top of asp.net core pipeline, and I've no problem with manually developed asp.net core middlewares too. My main problem is with some middlewares like mvc core, identity server, signalr etc.
As I understand it, ASP.NET Core has support for OWIN middleware (via app.UseOwin()) in addition to its own native middleware.
What is the difference between ASP.NET Core middleware and OWIN middleware?
When designing a new middleware, how do I know if I should design it as a ASP.NET Core middleware or a OWIN middleware?
Your question made me curious and I would like to share, what I have learned so far.
Katana is the implementation of the OWIN spec. After version 3.0 of Katana this technology has been fully integration in the web stack we know as ASP.NET Core today.
While this transition much has stayed similar to the OWIN specifications. Although some changes have been made. In order to use existing OWIN middleware in ASP.NET Core the supports OWIN by an optional feature ("app.UseOwin()").
If you want to target with your middleware ASP.NET apps and ASP.NET core apps, then I would use OWIN middleware. If you want to give ASP.NET Core developers a first class citizen experience, then a ASP.NET Core middleware would be recognized as more "fitting".
Some information about the relationship between ASP.NET Core middleware and OWIN middleware can be found here:
Katana, ASP.NET 5, and bridging the gap
Katana Project
https://docs.asp.net/en/latest/fundamentals/owin.html
I have come to understand it as this; ASP.NET Core middleware is on a higher level than OWIN middleware which is on a lower level.
ASP.NET Core middleware has the advantage that it is much easier to develop a middleware in as you get passed in the HttpContext which you can use. The disadvantage is that the middleware you develop depends on ASP.NET Core.
OWIN is on a lower level and you get a OWIN environment which is a IDictionary<string, object>. The advantage is that is it not tied to ASP.NET hence can run on any OWIN server (such as Nowin). The disadvantage is that it takes more effort to code since you have to build your own context from the OWIN environment or use the OWIN environment dictionary directly and keep track of all OWIN keys and objects.
Edit: You don't have to keep track of OWIN keys yourself, you can use the OwinEnvironment class to get a strongly typed environment.
var environment = new OwinEnvironment(HttpContext);
var features = new OwinFeatureCollection(environment);
When moving from OWIN to ASP.NET Core, I've found a bit of information about dependencies to migration, but I've not found information about these other topics:
The middle-ware pipeline. How is this different, or not?
The DelegatingHandler pipeline (e.g. Web API). How is this different, or not?
The startup.cs file. How is this different?
In short, it would be great to know what are the primary hot-spots that would need my attention, in order to move from OWIN to ASP.NET Core.
As a first example - ASP.NET Core does not appear to have HttpConfiguration. There are myriads of example plugins and services that assume its existence. I'd like to infer how to translate instructions for HttpConfiguration into whatever approach ASP.NET Core expects.
As another example, the Swashbuckle documentation gives instructions for setup with OWIN, but those instructions don't work with ASP.NET Core. If I understood the primary differences from OWIN, it would be easier to "guesstimate" how to install Swashbuckle into ASP.NET Core.
Note: Swashbuckle also gives instructions for self-hosted apps. That confuses me because I think of OWIN (vis-a-vis Katana) as being self-hosted already, and so it sounds redundant to me. But I don't think this is related to the present question.
My question has used Swashbuckle as an example, but I am not asking about Swashbuckle specifically.
Update
I've discovered that much of the information I'm looking for is in the article Transitioning from Web API 2 to ASP.NET MVC 6.
Middleware is quite similar between Katana and Core but you use HttpContext instead of IOwinContext.
Startup.cs is similar but there's much more DI support.
WebApi has been merged into MVC
DelegatingHandler is gone, use middleware instead.
HttpConfiguration has been split up into Routing and MvcOptions.
Also https://devblogs.microsoft.com/aspnet/katana-asp-net-5-and-bridging-the-gap/
I think you can start here. It's an entire chapter about OWIN with ASP.NET Core. Hope this helps.