Blog posts about asp.net

  • Show text line breaks in ASP.NET MVC Razor view

    December 10, 2014 | Tags: CSS, ASP.NET, MVC, Snippet

    If you have a string with line breaks and want to show that text with the line-breaks intact in your Razor-based view, you can do this without replacing all \r\n with <br/>-tags. Instead present the text in an element that has the style property white-space set to pre-line. You should really add a class like:

    <style>
    .line-breaks { white-space:pre-line; }
    </style>

    <span class="line-breaks">@Model.MyText</span>

  • HttpContext.Current not working in ASP.NET MVC

    December 09, 2014 | Tags: Troubleshooting, ASP.NET, MVC, C#

    This is most likely because it's no resolving to the correct class. Instead of using System.Web.HttpContext it tries to resolve towards System.Web.Mvc.Controller.HttpContext. The way to fix this is to prefix HttpContext.Current with System.Web so that your usage looks like: System.Web.HttpContext.Current


  • Get all model validation errors in ASP.NET MVC

    December 08, 2014 | Tags: C#, ASP.NET, MVC, Snippet, Validation

    In ASP.NET MVC you can get a list of all validation errors for your model from the ModelState:

      if (!ModelState.IsValid) {
        var modelErrors = new List<string>();
        foreach (var modelState in ModelState.Values) {
          foreach (var modelError in modelState.Errors) {
            modelErrors.Add(modelError.ErrorMessage);
          }
        }
        // do something with the error list :)
      }

  • Convert web application project to class library

    December 20, 2012 | Tags: ASP.NET, C#

    There's no way inside of Visual Studio to convert a web application project to a class library.

    However, this is pretty easy done using your favourite text editor of choice by editing the .csproj-file changing two lines.

    If you're  not totally confident with what you're about to do, make sure you start with making a backup!

    1. Open your project file (.csproj)
    2. Locate <projectguid> and make a small change to the GUID.
    3. Locate <projecttypeguids> and delete completely.
    4. Save and reload project in Visual Studio. It should load as a class library this time.


    Solution originally found here.


  • WCF and ASP.NET Compatibility Mode

    December 19, 2012 | Tags: ASP.NET, Troubleshooting, WCF
    Recently I had a troublesome error when developing a web hosted WCF service. The solution relied on some legacy components from an ASP.NET website - that is some cases throw errors, seemingly at random.

    After alot of trouble shooting I noticed that the problem only occured when - after building the project - I made the first request through a WCF client. The legacy components would then return errors until the project had been rebuilt. If I after rebuilding made a request to a test page within the WCF-project, the legacy components worked like a charm.

    Although I've done my fair share of web services in the past, this was my first WCF project. And there's a major difference between standard ASP.NET webservices and WCF.

    By default calls to the WCF service is run outside of the ASP.NET pipeline, in something called "Mixed Transports Mode". That means that it will bypass the ASP.NET pipeline and you'll loose things like HttpContext. Some of which the legacy components in my project apparently required.

    The solution was to switch to "ASP.NET Compability Mode". That means that the WCF request is treated as any other ASP.NET request.

    This is done by setting aspNetCompatibilityEnabled to true in the web.config:
    <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    ...

    And also decorate the WCF service class with the AspNetCompatibilityRequirements attribute:
    [ServiceBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

    public class UserService : IUserService {
    ...

    To learn more about this I recommend the following post about ASP.NET Compability Mode.