Blog: ASP.NET

NSwag Tutorial: Generate an Angular TypeScript client from an existing ASP.NET Web API web assembly

October 1, 2016, (updated on July 6, 2017), 11 comments, Software Development

This tutorial shows how to generate an Angular TypeScript client with the NSwag Swagger toolchain. You can use this client in your Angular 2 SPA (single-page application) to consume the web services of an existing Web API project. The metadata for the client generator will be loaded from an existing .NET assembly which contains your Web API controller classes. The NSwag project provides many other TypeScript templates (for targeting other JS frameworks) and also a C# client generator. Introduction video: For ASP.NET Core projects, I recommend to use the middleware to generate the Swagger spec (there are some problems loading […] Read more...

Tags: , , , , ,

NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project

September 30, 2016, (updated on October 1, 2016), Software Development

This tutorial shows how to integrate NSwag (Swagger toolchain for .NET) so that you can access your Web API actions with the Swagger UI and a generated C# client library. The toolchain can be integrated into the following three types of Web API projects: Global.asax-based Web projects (full .NET Framework) OWIN-based Web projects (full .NET Framework) ASP.NET Core based projects (full .NET Framework or .NET Core) For each project, the integration is a little bit different, but this tutorial will provide links to all supported ways. Just follow the steps. 1. Create and setup a new Web API project First […] Read more...

Tags: , , , , , , ,

How to programmatically retrieve the deployment time of an ASP.NET web site

May 26, 2016, (updated on August 24, 2017), Software Development

In one of my web projects, I wanted to append the web site deployment time to the JavaScript URLs. This way the browser uses the cached file version until a new web site version is deployed. To do so, I looked for a way to read the deployment time of the currently running ASP.NET web site. It’s actually very simple, just read the modified date of the applications Web.config: var configPath = Server.MapPath("~/Web.config"); var deploymentTime = File.GetLastWriteTime(configPath); Do you known a better way to do this? Update: Retrieve deployment time in ASP.NET Core [HttpGet("deployTime")] public DateTime GetDeployTime([FromServices]IHostingEnvironment hostingEnvironment) { var […] Read more...

Tags:

Precompile ASP.NET MVC Razor views in Release mode to find errors at compile time

A problem we often face when developing ASP.NET MVC applications is that errors in Razor views are only detected at runtime while browsing the site. This is because the Razor views are not compiled until they are requested by the browser. To avoid this problem, you can prebuild the views by adding the MvcBuildViews tag to your .csproj file: <?xml version="1.0" encoding="utf-8"?> <Project ...> ... <PropertyGroup> ... <MvcBuildViews>true</MvcBuildViews> ... There are two problems with this approach: The build process takes much longer You cannot change the views when debugging the running application To avoid these problems and still be able […] Read more...

Tags: , , , ,

Post-process the HTML output of an action in ASP.NET MVC

May 5, 2015, (updated on December 16, 2015), 3 comments, Software Development

This article shows how to implement a post-processor for an ASP.NET MVC application, which transforms the output of an controller action before it is transmitted to the client. The obvious way to do this is by implementing a custom action filter. First, we implement the base filter class: public abstract class OutputProcessorActionFilterAttribute : ActionFilterAttribute { protected abstract string Process(string data); public override void OnResultExecuted(ResultExecutedContext filterContext) { var response = filterContext.HttpContext.Response; response.Filter = new OutputProcessorStream(response.Filter, Process); } } As you can see in the OnResultExecuted method, the response’s current Filter property is wrapped by a new stream implementation. Using this interceptor […] Read more...

Tags: , , , ,

ASP.NET MVC: How to implement an edit form for an entity with a sortable child collection

March 30, 2015, (updated on June 10, 2015), 26 comments, Software Development

This article shows how to implement the edit form for an entity with a collection property. As a show case, we will implement the edit form for a person entity with multiple addresses. The following diagram shows the UML diagram of the two entity classes: The final edit form for a person entity should look like this: The goal is to implement this form so that the address items are sortable and validated directly on the page. The article describes how to implement the described edit form in a simple way with as little as possible lines of code. The […] Read more...

Tags: , , , ,

How to implement and register a custom dependency resolver in ASP.NET MVC 5

March 6, 2015, (updated on March 26, 2015), Software Development

The ASP.NET MVC 5 framework supports the integration of 3rd party dependency injection frameworks. This can be done by implementing the IDependencyResolver interface and registering an instance of the interface on application startup. The registration is made in the Application_Start method which can be found in the Global.asax.cs file: protected void Application_Start() { ... var compositionContext = new CompositionContext(); compositionContext.AddPart<IMyService, MyServiceImpl>(); // TODO: Register your own services here DependencyResolver.SetResolver(new MyDependencyResolver(compositionContext)); } Most dependency injection frameworks provide a NuGet package which already contains an IDependencyResolver implementation (for example Ninject.MVC3). However, because I wanted to use the IoC container from my own […] Read more...

Tags: , , , , , , ,


  Page 1 of 1