Solution: The file ‘.asmx’ has not been pre-compiled, and cannot be requested.

I had published/precompiled my site and deployed to IIS however when I try to use the service asmx I get the error “The file ‘.asmx’ has not been pre-compiled, and cannot be requested”.

In order to fix this issue I opened the “PrecompiledApp.config” and changed the updatable attribute as follows:

From:

<precompiledApp version=”2″ updatable=”false”/>

To:

<precompiledApp version=”2″ updatable=”true”/>

Hope this works for you too 🙂

Advertisements

ERROR: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.

This week I was working on a migration script where I needed to run multiple sql queries. Since I was going to use this only once, I decided to use SqlHelper classes to connect and run sql to MsSql2012. Part of this included inserting and updating a date into the database. On my local computer this was working fine however, when placed on the server, I kept on getting the below error:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.

A sample of the Sql I was trying to use was:

UPDATE Table SET StartDate = '26-Mar-15 2:53:42 PM' WHERE ID = 15

I googled this error and amongst the top suggestions/solutions I found:

  • sql query is passing the wrong date format so it goes out of range
  • you should not pass DateTime values as sting in your SQL statement. Instead use parameters such as (    cmd.Parameters.AddWithValue("@mvendid", mVendid);  )

None of the 2 above worked. I tried all sorts of formats (“dd/mm/yyyy hh:mm:ss”, “mm/dd/yyyy hh:mm:ss”, “yyyy/mm/dd hh:mm:ss”, etc etc). I tried also declaring a new date in the sql and saving it, which still gave same error. I was about to give up when I though what about the computer’s date?

Solution:

We checked my computer’s date format from “Control Panel -> Region and Language -> Formats” and noticed that the short date was completely different. Since I was desperate, we tried changing the server’s local date to match my date. Just like a miracle it worked fine.

Region

 

 

Hope this helped … as I was going mental for a while there!!!

A POTENTIALLY DANGEROUS REQUEST.FORM VALUE WAS DETECTED FROM THE CLIENT

Error / Cause:

In .NET we have Request validation, which is a feature to prevent the server from accepting content from the client side containing un-encoded HTML. It helps to prevent client script-injection attacks submitted to a server for malicious intent. Even thought the intension of such feature is good, it can also cause some issues for us. For instance, I am trying to pass xml data in an input field and the Request validation is preventing the page from proceeding to the server.

Solution:

  • When using up to .NET 2
    • Two options:
      • On the page you would like to run your un-encoded HTML, on the aspx page at the top (line 1 along with Page Language=”C#” ….) add: ValidateRequest="false"
      • Otherwise, if you would like to switch validation off globally for all pages, in your web.config under the system.web section add:
        <pages validateRequest="false" />
  • When using newer version of .NET
    • First do the either of the about to turn off validation.
    • In your web.config, find the HttpRuntime and set requestValidationMode to 2.0 as follows:
      <httpRuntime requestValidationMode="2.0"/>

 

This should do the trick!

Adding MVC Meta Tags to razor pages

In every View we can assign different meta tags (title, description, keywords, etc) however it is not very clear to every one how this is achieved. In reality this is quite easy.

So here it goes:

In your “_Layout.cshtml” paste the following in your head section:

@if(ViewBag.Description!=null)
{
<meta name=”description” content=”@ViewBag.Description” />
}
@if(ViewBag.Keywords!=null)
{
<meta name=”keywords” content=”@ViewBag.Keywords” />
}

 

Now, in your other pages which will inherit from the layout, at the very top where you usually have Viewbag.Title defined, add the following Viewbags:

@{

ViewBag.Title = “Home Page”;
ViewBag.Description = “Welcome to Rochcass Blog”;
ViewBag.Keywords = “C# posts, errors and solutions, problem solving, css3, html5, ASP.NET, Linq, MVC, Nhibernate”;
}

 

You can do the same for other meta tags such as author, etc.

 

 

 

 

Adding mime types to web.config

In some cases, we need to add mime types in order for the IIS to detect our files. For instance, I needed to add a custom font using CSS3 and so I added it to my stylesheet as follows:

@font-face {font-family: “myCustomFont”; src: url(../fonts/myFont.otf) format(“opentype”);}

When running on my Localhost, the font would appear fine however when upload to IIS the fonts would not show properly.

It also makes a difference depending which IIS one is using.

If running on IIS 6 or older, you will need to configure mime types on the server:

Read Microsite Instructions on configuring mime types for IIS6

If running on IIS 7 or newer, the mime types can be added to the web.config which will allow you to not touch the IIS directly (suggested):

Open the web.config and in the <system.webServer> section which is situated directly within <configuration> tag and add the following mime types to cover it all:

<staticContent>
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />
<mimeMap fileExtension=".m4v" mimeType="video/m4v" />
<mimeMap fileExtension=".ogg" mimeType="video/ogg" />
<mimeMap fileExtension=".ogv" mimeType="video/ogg" />
<mimeMap fileExtension=".webm" mimeType="video/webm" />
<mimeMap fileExtension=".oga" mimeType="audio/ogg" />
<mimeMap fileExtension=".spx" mimeType="audio/ogg" />
<mimeMap fileExtension=".svg" mimeType="images/svg+xml" />
<mimeMap fileExtension=".svgz" mimeType="images/svg+xml" />
<remove fileExtension=".eot" />
<mimeMap fileExtension=".eot" mimeType="application/vnd.ms-fontobject" />
<mimeMap fileExtension=".otf" mimeType="font/otf" />
<mimeMap fileExtension=".woff" mimeType="font/x-woff" />
</staticContent>

Hope that helps 🙂

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 🙂