Dailycode.info

Short solution for short problems

AngularJS - WebAPI: No 'Access-Control-Allow-Origin'

Got this error: XMLHttpRequest cannot load http://localhost:777/api/products/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:777' is therefore not allowed access. The response had HTTP status code 405.

It was not so easy to sfind out what the problem was, but then it had something to do with enabling cross origin requests. When you are developing WebAPI back end that is not on the same uri as the front end angular JS site, it could be that you have this error. I even got it on my local host, using different ports for the 2 sites. Simple solution, which will help to secure the WebAPI.

You will first have to allow the cros origin requests in your WebApiconfig.cs:

config.EnableCors();

If VS gives problems, then use the nuget package manager to add this


Then first add this code to the WebApiConfig:

config.EnableCors();

And then you will have to allow ip to connect to your webapi in your controllers like this:

// GET: api/Products/Mark  
[EnableCors("http://localhost:778","*","*")] 
public List<Product> GetProduct()
You can also allow all methods in your API by putting the EnableCors attribute above the class:

using System.Web.Http.Cors;

namespace MyProject.Controllers

{

    [EnableCors("http://localhost:778", "*", "*")]

    public class UsersController : ApiController


iOS: How to add a precompiled header file

Sounds stupid, since before this was default behaviour. Now a project starts without. Adding can sometimes be a pain, since just adding the file is not enough. 

This post helped me to the solution, although I head to change 1 more thing.

So the thing was to open the Build settings in xcode of your target. You will see a search box on the upper right corner.

Search for: GCC_PRECOMPILE_PREFIX_HEADER and set it to Yes.

Next search for: GCC_PREFIX_HEADER and add the name of the file including the extension ".pch".

Now it was still not compiling, because the file was not in the root directory of where the project file was, but rather in a subdirectory. So I had to put in the value:

MyProgram/MyProgramPrefixHeader.pch

Now everything compiled again and the prefix header is loaded!


LINQ: Left outer join (non relational DB)

If you want to get data en detail data, from for example Routes. You will need to select the data and the details and then select only the distinct records. If your model framework support relations (Foreing key) then it easier to accomplish with LINQ. But in my case the relations were not set in the object model.

So this is how I get all routes with a detail that have a certain status, or routes that have no details:

var routes = from r in Ents.Routes

                from rd in Ents.RouteDetails.Where(s => s.RouteID == r.RouteID).DefaultIfEmpty()

                where (rd.Status == 10 || rd.Status == null)

select r;

 

var distinctRoutes = routes.Distinct<Routes>();

I cannot use the join with LINQ because there are no relations between the objects, so I select all routes with details that have status 10 or routes that have no details. The .DefaultIfEmpty will add an empty RouteDetail object to the list if it has no detail. So in the were I can check for null, then it includes the Routes that have no details. Now there can be duplicates because some routes have more detials. If we only select the Routes (select r) then the object with different details have the same Route object, so the distinct at the end will clear the duplicates.

 


VS 2010/2012 crashes when opening designer.

This can be a really time eating problem. In my case this problem came suddenly, in a class that didn't change for a long time. 

The error looked something like this:

Faulting application name: devenv.exe, version: 10.0.40219.1, time stamp: 0x4d5f2a73

Faulting module name: KERNELBASE.dll, version: 6.1.7601.18409, time stamp: 0x53159a86

Exception code: 0xe0434352

Fault offset: 0x0000c42d

Faulting process id: 0x1898

Faulting application start time: 0x01d00d526a76a093

Faulting application path: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe

Faulting module path: C:\Windows\syswow64\KERNELBASE.dll

Report Id: f6f5bf23-7945-11e4-afad-02004e435049

When you experience this kind of behavior, it could be that an add-in is causing the problem or some update of software. In our case this didn't help.

So at the end we found that is was a problem in the page load of a form, it gets executed when you open the designer. We had some code that went to the database. The strange this is that this worked for years and now suddenly it starts crashing on all development PC's. The solution to this problem is to prevent the code from being run at design time by using the System.ComponentModel.LicenseManager.UsageMode:

Private Sub BaseForm_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Handles Me.Load

    Try

       If System.ComponentModel.LicenseManager.UsageMode = System.ComponentModel.LicenseUsageMode.Runtime Then

          HandleFormDisableButtons()

          Translate()

       End If

    Catch ex As Exception

ExceptionHandling.ShowException(ex)

    End Try

 

End Sub