Asp.Net Helpers Small Helper functions and classes (not only) for MVC and Webapi

Asp.Net Helpers

  • Tuesday
  • 10.03.2015
  • Asp.Net Helpers

Asp.Net Helpers

The Helpers are a small lib providing you several features which I found useful during developing applications.

Download AspNet-Helpers

IResult Interface

public IHttpActionResult GetAllItemsForProject(int id)
    IResult<list<ItemsForProjectViewModel>> result = _myServiceWhichDoesTheWork.DGetItems(id);
    if (result.IsValid)
        return Ok(result.Value);
    return BadRequest(result.Error);


The IResult Interface is usable for giving you one interface to have a result of an action which can be successfull or not. Depending on this you can react and possibly return an Ok(...) or a BadRequest(...).

ViewModelBase / SubmitModelBase

public class AccountRegisterViewModel : ViewModelBase
    public AccountRegisterSubmitModel SubmitModel { get; set; }
    public AccountRegisterViewModel()
         Url = "~/Here/Goes/My/Url.cshtml";


The ViewModelBase represents a base class for a ViewModel. It contains info-, error- and successmessages which can be filled up by you and you can access them on the client easily.


public AccountRegisterViewModel RegisterUser(AccountRegisterSubmitModel submitModel)
    AccountRegisterViewModel viewModel = GetAccountRegisterViewMmodel(submitModel);
        if (!_modelValidator.IsSubmitModelValid(viewModel, submitModel))
            return viewModel;
    // Do some stuff
    catch (Exception)
        viewModel.ErrorMessages.Add("There was an error. Can not register.");
    return viewModel;

The ModelValidator is a helper which takes a viewmodel and a submitmodel as parameter. It checks if the submitmodel is valid and if not, it fills up the errormessages of the viewmodel automatically. This makes your code more clean and readable.

Check for null

//Throws an exception
Person person = null;

Throws an ArgumentNullException if value is null


Person myPerson = person.CheckIsNull("Person must not be null");

Throws an ArgumentNullException if value is null. If not it returns the specific type


bool personIsNull = person.IsNull();

True if value is null, otherwise false


person = new Person() { Id = 1, Name = "Fabian" };
List<person> listOfPersons = new List<person>() { person };
//should be false
bool isNullOrEmpty = listOfPersons.IsNullOrEmpty();

Checks enumerable on null and being empty.


string timeSpanToCheck = "11:20";
bool isTimeSpan = timeSpanToCheck.IsTimeSpan();

True, if values is a valid TimeSpan, otherwise false


// Checks if a string is a valid email or not
string email = "";
bool isEmail = email.IsEmail();

Checks if a given string is a valid Email-Adress


//Converts a string to datetime
string toDateTime = "01.01.2010";
DateTime dateTime = toDateTime.ToDateTime();

Converts a string to DateTime, if not it throws an exception


// Converts a string to Timespan
timeSpanToCheck = "11:20";
TimeSpan timeSpan = timeSpanToCheck.ToTimeSpan();

Converts a string to TimeSpan, if not it throws an exception


//Checks if a password has a specific length or is null or empty
string password = "TheseAreAtLeastEightSigns";
bool isValidPassword = password.IsValidPassword(8);

Checks if a string is not null,filled and has a specific lenght. False if any of these conditions are not fulfilled, true if everything matches


// Checks if a file has a given format...
HttpPostedFileBase httpPostedFile = ...
bool hasFormat = httpPostedFile.HasFormat("txt");
// ... or wth multiple extensions
hasFormat = httpPostedFile.HasFormat("txt", "pdf");

Checks if a file has a specific format


// Checks if a file is an image...
bool isImage = httpPostedFile.IsImage();
// ...also with formats
isImage = httpPostedFile.IsImage("jpg", "gif");

Checks if a file is an image (Checks the contenttype) and checks if the image has a valid format


//Checks if a value is a double value (separation with . or ,
string doubleToCheck = "3.5";
bool isDouble = doubleToCheck.IsDouble();

Checks if a string represents double


//Converts a string to double
doubleToCheck = "3.5";
double doubleValue = doubleToCheck.ToDouble();

Converts a string to double. If string is invalid an exception is thrown