What is the purpose of AddFormatterMappings() on Asp.net Core 2 - asp.net-core

I am evaluating the configuration of a Web API App with Asp.Net Core 2. To configure I know it is better for my project to use .AddMvcCore() rather then use AddMvc() since I don't need Razor as we can see here.
Now, I am not sure if I also need this .AddFormatterMappings(). So my question is what is it for?

You can see from the source code in the MVC GitHub repo that it adds a FormatFilter service into the DI setup. The summary for the FormatFilter class is as follows:
A filter that will use the format value in the route data or query
string to set the content type on an ObjectResult returned from an
action.

Related

ASP.NET Transfer data from controller action

There is a sales service implemented as a Telegram bot. I need to create a website control panel for this service. Since the service is a .NET application I am thinking to use ASP.NET Core technology.
How do I transfer data from the controller action to the Program class containing all the functionality of the service (maybe it is worth defining the Program as a static class)?
You may have misunderstood Asp.Net Core. .net core adopts the pipeline mode, that is, when you call the action in the controller, it will enter the middleware pipeline of Program.cs(.net 5 is Startup.cs), and execute in sequence according to the order of your middleware, adopting the principle of first in, last out. This means that if you follow the normal .net core logic, the value you get in the controller (except the parameters defined in the URL), you cannot pass it into Program.cs. When you successfully enter the action of the controller, Program.cs has been executed.
Not sure what your sales service looks like, but I think you can register it as a service and use it in your controllers using dependency injection.
Helpful link: ASP.NET Core Middleware.

How to add logging to WCF?

I've used the solution from here for logging (and insert to DB) REST request successfully, now i have to do the same to WCF
[LogApiRequest]
public NadlanData GetNadlanData(decimal Id, KodFamilyEnum KodFamily)
{
ClsDalByTz objByTz = new ClsDalByTz();
return objByTz.getDataFromMF(Id, DateTime.Now.Year - 5, (decimal)KodFamily);
}
I've tried to do the same by adding [LogApiRequest] to the WCF function but it did not work.
How can i implement the same solution for WCF?
The custom attribute - LogApiRequest - derives from System.Web.Http.Filters.ActionFilterAttribute. Action filter attributes are documented here - https://www.tutorialsteacher.com/webapi/web-api-filters and here - https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs.
The reason your attribute works in your web api is because asp.net or asp.net core processing pipeline supports calling custom code defined in user defined code - you are plugging custom feature in the processing pipeline.
Know more about asp.net pipeline here - https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/september/asp-net-request-processing-filtering-and-content-redirection
The reason it does not work with WCF is because that processing pipeline does not support customisation through HTTP filters.
WCF has its own way of doing logging. Check out these links - https://learn.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/tracing/configuring-tracing and https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/tracing-and-message-logging
You will find yourself editing custom configuration files most of the time.
I cannot give you a short attribute name or configuration script because it depends on type of WCF endpoint you are implementing, their name etc. The tracing and message logging link above contains some samples. Copy paste those configurations but edit file path and interface names based on what you have got code. That should work.

Http handler in asp.net core

Now i have asp mvc 5 .net framework project and handlers (ashx files with custom logic) to process customer needs (i.e. pricelists in custom format).
I intend to move to asp core, and the question : is there an equivalent to handler?
Articles on internet suggest to put logic to middleware, but this is inconvenient way in my case.
Can you explain what do you need that middlewares cannot provide them for you?
Middlewares are built in a way that you can completely migrate from old Http Handlers/Modules to them.
https://learn.microsoft.com/en-us/aspnet/core/migration/http-modules?view=aspnetcore-2.1
Razor pages with it's own logic and model best way to substitute custom handlers in my case
You could use Middleware or Interceptor for tracking any incoming request.
The most important difference between them is that, you could use interceptor per any action too - for this you should use dependency injection.

Replacement for HttpHandlers in ASP.NET vNext

I have read that HttpHandlers is not part of ASP.NET 5 (vNext)Is there a replacement tha can be used instead that works the same.
I am looking for a solution that can load an image based on id from a entity. And if that image doesn't exist, a [Non Image] Image should be shown instead. This works perfect with http handler. But I don't see a good solution in ASP.NET vNext.
The replacement of HttpHandler and HttpModule is middleware. You can easily wrote a middleware to handle your issue.
However in your particular case a custom Image TagHelper can be more usefull.
TagHelper sample
Middleware sample

Ninject dependency into AuthorizationAttribute MVC4 Web API RC

I have a custom authorization attribute, required only for some actions, which checks the request headers for a custom token. The token is checked in a database. Checking the database requires access to a service which I would like to have injected through the constructor.
The way I have read this can be done (here, here, and here) is by having a constructor-less filter and injecting the dependent one like this:
kernel.BindFilter<MyAuthorizeFilter>(FilterScope.Controller, 0).WhenControllerHas<MyAuthorizeAttribute>();
However the BindFilter method is not available to me as I have setup Ninject as described here. This is using Ninject.Web.Common instead of Ninject MVC3 as I read that Ninject MVC3 would not work with MVC4 RC. How else can I go about accomplishing this?
I have read also that I could add to GlobalFilters.Filters - however I don't want it to be present on every action.
Thanks in advance for any answers.
I'm not completely sure I see how you have set up your application, but my experience has been that if you want a filter for a WebApi controller you need to add it to the HttpFilterCollection that is available from the GlobalConfiguration.Filters. This is a Different set of filters than what MVC uses (through the GlobalFilterCollection).