Which can even lead to bugs
Source: Top Misconceptions About Dependency Injection in ASP.NET Core | by Sasha Mathews | Level Up Coding
My bookmarks and blogposts regarding Software Development in .NET, C#, Angular, JavaScript, CSS, Html
Which can even lead to bugs
Source: Top Misconceptions About Dependency Injection in ASP.NET Core | by Sasha Mathews | Level Up Coding
So, in order to be able to modelbind XML to a class without taking namespaces into consideration I created new InputFormatter. And I use XmlTextReader in order to ignore namespaces. Microsoft recommends to use XmlReader rather than XmlTextReader. But since XmlTextReader is there still (in .Net 6.0 Preview 3) I’ll use it for now.
Simply create an inputformatter that inherits from XmlSerializerInputFormatter like so:
public class XmlNoNameSpaceInputFormatter : XmlSerializerInputFormatter
{
private const string ContentType = "application/xml";
public XmlNoNameSpaceInputFormatter(MvcOptions options) : base(options)
{
SupportedMediaTypes.Add(ContentType);
}
public override bool CanRead(InputFormatterContext context)
{
var contentType = context.HttpContext.Request.ContentType;
return contentType.StartsWith(ContentType);
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
var type = GetSerializableType(context.ModelType);
var request = context.HttpContext.Request;
using (var reader = new StreamReader(request.Body))
{
var content = await reader.ReadToEndAsync();
Stream s = new MemoryStream(Encoding.UTF8.GetBytes(content));
XmlTextReader rdr = new XmlTextReader(s);
rdr.Namespaces = false;
var serializer = new XmlSerializer(type);
var result = serializer.Deserialize(rdr);
return await InputFormatterResult.SuccessAsync(result);
}
}
}
Then add it to the inputformatters like so to startup.cs:
services.AddControllers(o =>
{
o.InputFormatters.Add(new XmlNoNameSpaceInputFormatter(o));
})
.AddXmlSerializerFormatters();
Now we can modelbind Person or any other class no matter if there is namespaces or not in the incoming XML. Thanks to @yiyi-you
Source: c# – Ignore XML namespace when modelbinding in asp net core web api – Stack Overflow
This is taken from Jon Hilton, Quick Tip Friday newsletter.
You can subscribe to the newsletter here: https://jonhilton.net/
Its a great tip regarding example code for popular web frameworks (also mobile in the works), for example ASP.NET Core on the backend side and Angular or Vuejs on the frontend side etc. See list of backends and frontends here: (scroll down there are many options…)
https://github.com/gothinkster/realworld
Building software applications is hard, but doing it in isolation is even harder.
And, unless you’re one of the fortunate few who work in a perfectly balanced team with just the right amount of experienced developers to lean on, you’re often left figuring all this stuff out for yourself.
· Architectural best practices
· How to organise your code
· How to separate your concerns
· Which ORM to use
· How to KISS (keep it simple stupid) and avoid incurring loads of technical debt
But, just because you haven’t got a supportive, experienced and wise team physically on your doorstep doesn’t mean you have to re-invent all the wheels yourself!
Specifically I really want to recommend you check out the “real world” examples over at https://github.com/gothinkster/realworld.
There, they take the exact same application (a Medium clone) and show examples built in all manner of front-end and back-end frameworks.
So if you’re looking to see what a good ASP.NET Core API application looks like (or React, Angular, Vue.js or anything else you can think of)
When you reference a .js framework or library from your ASP.NET application, it makes sense to use the development version when you’re working on your own machine.
Referencing the development versions brings more useful error messages and integration with the React Developer tools.
But, you wouldn’t want to use these in production for performance reasons.
When you run the site in production, you’d want to reference the minified production versions of the React scripts.
Source: Different js scripts for different ASP.NET core environments· jonhilton.net – Making sense of .NET
“Razor Pages are a new feature in ASP.NET Core 2.0. They provide a simpler way to organize code within ASP.NET Core applications, keeping implementation logic and view models closer to the view implementation code. They also offer a simpler way to get started developing ASP.NET Core apps, but that doesn’t mean you should dismiss them if you’re an experienced .NET developer. You can also use Razor Pages to improve the organization of larger and more complex ASP.NET Core apps.”
Source: ASP.NET Core – Simpler ASP.NET MVC Apps with Razor Pages
.NET Core and ASP.NET Core offer several advantages over traditional .NET development. You should use .NET Core for your server applications if some or all of the following are important to your application’s success:
Cross-platform support
- Use of microservices
- Use of Docker containers
- High performance and scalability requirements
Side-by-side versioning of .NET versions by application on the same server
Online: Architect modern web applications with ASP.NET Core and Azure | Microsoft Docs