Error: Request format is unrecognized for URL unexpectedly ending in ‘/myMethod’.

Scenario:

I have created a Web Application with an “asmx” page to allow web methods (Insert, Update, Delete) to act as a web service. I used a jQuery Post to communicate with the Web Method and all worked fine while working on my localhost. Once the Web App was deployed to server, when I try to access the Web Method from the browser example: “myDomain.com/myPage.asmx/Insert”, I kept on getting an error saying: “Request format is unrecognized for URL unexpectedly ending in ‘/Insert’.”

Cause:

Apparently this was caused due to cross domain access origin issues. Cross-domain origin does not allow one domain to call a page/method from another domain. The Web Methods needed to allow access for cross-domain calls.

Solution:

Step 1:

In my web.config, I add the following section under the section:

<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>

Step 2:

I created a global.aspx page and added an Application_BeginRequest method. In this method I declared access to cross-domain as shown below:

protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
}

Hope this helps 🙂

Error: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

I was trying to make an AJAX Post to another domain http://freegeoip.net/ to get Geo Location and therefore the browser would not allow me to proceed due to cross-domains. Cross domain means that if you are on domain A.com and would like to make a request to domain B.com, it does not allow the request to be made since the boundaries are set on the domain A.com by your browser. Script tags allow us to overcome this boundary however we still cannot read or do anything with the data returned.

Ajax Post:

$.ajax({

url: 'http://freegeoip.net/json/',
type: 'POST',
dataType: 'json',
success: function (data) {

var ip = data["ip"];
var country_name = data["country_name"];
var city_name = data["city"];

}

});

Error:

XMLHttpRequest cannot load http://freegeoip.net/json/. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:4868‘ is therefore not allowed access.

Solution ( Very Easy!) :

JSONP is just like the normal JSON we are used to but it also passes an extra parameters with more of your pages details. When sending a request to another domain B.com with JSON, if the domain B.com has JSONP enabled, it will change the response in a way to be readable for us to handle. Therefore, all we need to do is change the dataType from “json” to “jsonp” as follows:

$.ajax({

url: 'http://freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function (data) {

var ip = data["ip"];
var country_name = data["country_name"];
var city_name = data["city"];

}

});

This worked perfectly for me, and I could successfully get the Client IP, Country and City.

PS: the above requires jQuery to function since we are using a jQuery Ajax Request.

Goodluck 🙂

Solution: Unrecognized attribute ‘targetFramework’. Note that attribute names are case-sensitive.

Issue:

I was using Visual Studio 2012 and was writing a Web App using .NET 4.5 so in my web.config I had:

<compilation debug="true" targetFramework="4.5">

However every time I run my app, I was getting an error “Unrecognized attribute ‘targetFramework’. Note that attribute names are case-sensitive.”

This is caused by the IIS running an older version of ASP.NET

Solution to fix on local dev box:

  • Click on “Website” tab in the top menu
  • Select “Start Options..”

Website-StartOptions

  • From the pop-up window, on the left menu select “Build”
  • Choose your .NET framework desired
  • Click OK/Apply

.NET_framework

That should be it 🙂

WinSCP authentication using private key C#

Environment:

I was trying to connect to WinSCP from my Web Application using C# however I kept on getting an unknown server error. After doing some research (of which I did not find much), my dear friend helped me out figuring out the solution. So here it goes..

Error:

winscp> open "name@host.com:22" -privatekey=C:\keys\private.ppk
Searching for host...
Connecting to host...
Authenticating...
Continue connecting to an unknown server and add its host key to a cache?
The server's host key was not found in the cache. You have no guarantee that the server is the computer you think it is.
The server's dss key fingerprint is:
ssh-dss 1024 68:57:83:42:c9:42:08:db:13:0d:80:ec:8b:e2:43:c0
If you trust this host, press Yes. To connect without adding host key to the cache, press No. To abandon the connection press Cancel.
(Y)es, (N)o, C(a)ncel, (C)opy Key: Cancel
Host key wasn't verified!
Host key fingerprint is ssh-dss 1024 68:57:83:42:c9:42:08:db:13:0d:80:ec:8b:e2:43:c0.

Authentication failed.

Continue reading

Solved: Fluent Nhibernate SaveOrUpdate not updating DB

I had a scenario were all the select commands were working but all update commands (including delete) effecting db were not working. No errors or exceptions were being returned so it was quite hard to understand what it was. However finally, I realised that the problem was that nhibernate often commits updated once the session has been flushed.

To test if this is the issue:

After the SaveOrUpdate() command, I tried the session.flush(), and the db saved successfully.

To fix, visit the global.asax and in the Application_EndRequest add:

first add reference to:

using NHibernate;
using NHibernate.Context;

Then add the Application_EndRequest() as below:

protected void Application_EndRequest()

{

ISession mySession = ManagedWebSessionContext.Unbind(HttpContext.Current, SessionManager.SessionFactory);

if (mySession != null)
{

mySession .Flush();
mySession .Close();

}

}

If you are using transactions, I would suggest checking from active session transactions before flushing the session:

protected void Application_EndRequest()

{

ISession mySession = ManagedWebSessionContext.Unbind(HttpContext.Current, SessionManager.SessionFactory);

if (mySession != null)
{

if (mySession .Transaction != null && mySession .Transaction.IsActive)
{

mySession .Transaction.Rollback();

}
else
{

mySession .Flush();

}
mySession .Close();

}

}

IE Flash z-index problem – Solution

Problem:

I was trying to open fancybox ontop of a page with a flash player. The border and sometimes all the iframe was being displayed beneath the flash player in Internet Explorer, making it very ugly and impossible to use. Setting z-index to the flash player did not work.

Solution:

Wrap the flash player in a divider and add styling “z-index: 0” to the container and add an extra parameter to the flash player “wmode” with value “transparent”.

Example:

<div style=”z-index: 0;”>

</div>

Solved: Bootstrap 3.0.0 responsive navbar toggle not expanding/collapsing

I have recently downloaded bootstrap and needed to use a fixed responsive navbar at the top. When the window was small, the navbar was responding successfully as in only the logo and the toggle bar were showing rather than all the menu items. However, when clicking the toggle button, the menu items should have expanded down but instead nothing was happening.

I researched the issue on Google (as usual) but could not find a solution. Then I realised the Bootstrap needed jquery which I was infact using. However, I was not using the latest version. Therefore, I downloaded the latest version and tried it out. This seemed to have fixed my issue.

Hope it was of any help for some of you out there 😉

Error: [System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

When trying to download a file using similar code to the below, I kept on getting the ThreadAbortException:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader(“content-disposition”, attachment);
HttpContext.Current.Response.ContentType = “text/csv”;
HttpContext.Current.Response.AddHeader(“Pragma”, “public”);

var sb = new StringBuilder();

foreach (string row in rows)
{
sb.Append(row + “\r\n”);
}

HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.End();

Even if I surround it with try and catch, I was not solving my issue since I simply do not want the exception to occur and not go around it. After some research I discovered that reason was that Response.End ends the whole page execution and fires the Application_EndRequest event. Cutting the story short, replacing Response.End with the below solved my issue since it does not fire the Application_EndRequest event and continues to execute my page:

HttpContext.Current.ApplicationInstance.CompleteRequest();

MVC 4.0 jquery object has no method “”

In order to help you understand the reason for this post, I will explain my initial situation. I was using .NET MVC 4.0 with razor and I needed to use some jquery plugins. I tried fancybox, tagsinput, etc etc and each plugin kept on failing to work do the error similar to “$().fancybox is not a function”. After some reseach and attempts to fix this issue, I realised that jquery was loading twice, not because I referenced it twice.

I initially looked at the _layout.cshtml page and saw that css and modernizer are being rendered by razor but since I did not see the rendering of jquery I added my own jquery reference. However razor was rendering jquery at the very bottom of the _layout.cshtml and thus jquery was being referenced twice, making plugins unusable. Razor uses the follow render syntax to reference jquery at the bottom of the page:

@Scripts.Render(“~/bundles/jquery”)

To fix either remove your reference at the top or if like me you have used normal jquery such as $(document).ready() etc, you will need the reference at the top and not at the bottom. In that case either remove the render from the bottom, or just drag it all the way to the head of the page.

Hope this helped 🙂

XML – Domains, protocols and ports do not match

Error:

I was trying to load an xml stylesheet called xsl and I kept getting the following errors:

on Chrome: Unsafe attempt to load URL chrome-extension: .xsl from frame with URL test.xml. Domains, protocols and ports must match.

on Firefox: Error loading stylesheet: An unknown error has occurred (805303f4)

Cause:

Each browser has some security policies which do not allow access to different domains, protocols or ports are seen above.

Solution:

Allow access using cross domain policy. There are two ways of doing so:

Continue reading