How to exclude Json properties from WebApi response using a custom ContractResolver

I am currently working on a Backend API that acts as a middleman to a Front API and a DocumentDB database (the structure is shown in the above image).

 

I had the following class which is used to send data to both the Front API and to the DocumentDB. Some of the fields needed to be hidden when returning a response back to the Front API, since they weren’t useful for the user and for security concerns. First I tried using [JsonIgnore] and [IgnoreDataMember] (as seen below), which successfully excluded the required fields from the response, but using them was also excluding the same fields when sending the data to DocumentDB, so it was a no-go. Continue reading

The correct way to implement StructureMap in .NET Core WebApi when having multiple projects

StructureMap is an IoC/DI container for .Net which has been a long time around. I’ve been using it for these past few years, and after all this time, I found out the correct way to implement it.

This is how I used it before and I’m sure the majority of you are currently using the same methods. In the Registry, I would register each interface and it’s inherited class by calling a “config.for” for each service as follows:

    config.For(typeof(ITestService)).Add(typeof(TestService));
    config.For(typeof(IUserService)).Add(typeof(UserService));
    config.For(typeof(ITenantService)).Add(typeof(TenantService));
    config.For(typeof(IProjectService)).Add(typeof(ProjectService));

And whenever new interfaces and services are added, they need to be registered using a new “config.for” line.

That was the old way, of how I did things. This is the new way. Continue reading

Fix Upload_Max_Filesize for WordPress on Windows Hosting

upload-icon-20606

 

I needed to upload a file on a WordPress website, and the following issue was showing up:

The uploaded file exceeds the upload_max_filesize directive in php.ini

 

All the solutions I found show how to update the php.ini file, but this only works on Linux Hosting. For Windows Server and Windows Hosting accounts, you need to follow the next steps to solve the issue: Continue reading

Fixing AngularJS + MVC Routing Issues

hashratestore_icon-1Currently, I’m working on a hybrid AngularJS and MVC project, one of the tasks being to get rid of the # symbol from the URL. In pure AngularJS apps, this is very easily done by following the next two steps (and easily googled):

  1. Set up your base href in your index page: <head> <base href=”/”> </head>
  2. Add the following in your angular app config: $locationProvider.html5Mode(true)

 

The problem arises when you are in the .NET stack and using AngularJS with MVC. The above solution will still remove the # from URL, but page refresh won’t work. Why? This happens because MVC knows nothing about AngularJS routing and by following default MVC routing, it will look for an MVC page which doesn’t exist. Continue reading

Convert Enum to KeyValuePair List and remove unwanted Enum options #Linq

In our new project, using Angular, I needed to fill drop down lists in a form from a set of enums. One particular case was to set the below Status Enum as a drop down list.

    public enum Status
    {
        Deactive = 1,
        Active = 2,
        Expired = 3,
        Deleted = 4,
    }

To do that, the best solution I found was to convert the enums to a list KeyValuePair items. How do you do that? Continue reading

Create iOS and Android apps using Visual Studio and .NET

IC764760

Microsoft today announced some amazing news:

First the big news. Microsoft just launched the preview versions of Visual Studio 2015 and .NET 2015. This release includes the “Roslyn” .NET compiler platform, new language features in C#, improved debugging and profiling, and code analysis tools. And the biggest new of all – VS2015 supports “C#, C++ and HTML/JavaScript development targeting iOS, Android, Windows and more.” There’s even a Visual Studio Emulator for Android (see picture below).  I’m drooling! Continue reading

Xaml – Add and Update Row Definitions

XAML-LOGO Row and Column Definitions are used to set the rows and columns in a grid on a xaml page. I wanted to add or remove any of these definitions from the code behind using C#. Next is the solution:

Add Row Definitions:

RowDefinitionCollection defs = myGrid.RowDefinitions;
defs.Add(newRowDefinition(){Height=newGridLength(140)});
defs.Add(newRowDefinition(){Height=newGridLength(1,GridUnitType.Star)});

Update Row Definitions:

RowDefinitionCollection defs = myGrid.RowDefinitions;
defs[0] = newRowDefinition(){Height=newGridLength(140)};
defs[1] = newRowDefinition(){Height=newGridLength(1,GridUnitType.Star)};

 

To add and update columns, just change RowDefinition with ColumnDefinition in the above code. Hope this helps you in your Windows Phone and Windows Store apps! Happy Xamling!

MVC ActionLink not working after submit

These past days, I was having a problem with links for an iPad web app. The problem was that when I submit a form (step 1) and go to the next form (step 2), the link to return back to step 1 was not working. Apparently the problem is caused by Ajax. The solution is the following, instead of using the normal form tag to init a form:

      ……

use this type of form initialiser

@using (Html.BeginForm(“action”, “controller”, FormMethod.Post))
{

      ……

}

And add the following attribute:

{ “data-ajax”, “false” }

So the final code would look like this:

@using (Html.BeginForm(“action”, “controller”, FormMethod.Post, new Dictionary { { “data-ajax”, “false” } }))
{
……

}

Hope this post helps you, and reduces your swearing 🙂 Happy Form-ing!

 

 

 

Delete Operation in MVC

This week I just started some MVC tutorials which I’m really liking. And after some tutorials I found this “Performing a delete operation in response to a GET request (or  for that matter, performing an edit operation, create operation, or any other  operation that changes data) opens up a security hole”.

The problem is that if you create a link with the following href: www.mydomain.com/Movies/Delete/23, it will delete the movie with the passed id 23. If a hacker finds this, he can create a loop and delete all of your movies in the database.

The best solution would be to delete an item in an HTTP POST operation instead of the get, so the user must submit a form to delete an item. This is done using either buttons, or else images using the following markup:

                <% using (Html.BeginForm("Delete", "Home", new { id = item.Id }))
                   { %>
                    <input type="image" src="Content/Delete.png" />
                <% } %>

This will create an image link in a form which would POST the delete operation. This will reduce the security hole in your system. I suggest you read this article by Stephen Walter. Hope this will help you in your new MVC adventures.