ASP.NET MVC AJAX Sys is undefined error - asp.net

I am getting a "Microsoft JScript runtime error: 'Sys' is undefined" error on one of my pages in an MVC application when I attempt an AJAX call. The AJAX call is made from a partial view which is embedded in more than one page. It works fine on all of the pages except one. I have read posts pointing to the web.config file settings and .axd mappings as possible solutions, but the application is configured correctly in the web.config, and the .axd mappings are also correct in IIS. Plus it works fine on all of the pages that use this partial view except one. It is acting like the AJAX libraries are not loading for this one page.
The references to the script files are in the shared site.master file. All of the pages, including the one that doesn't work, reference the same masterpage.
Any ideas? I have been working on this one for two days now. Thanks.
EDIT: As Sam pointed out below, it would seem like the AJAX call is firing before the AJAX libraries have a chance to load. But, the AJAX call is triggered by a submit button long after the page has rendered, so the AJAX libraries have had plenty of time to load - sorry for not giving enough information the first time.

In web.config add the following line of code under appsettings tag:
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

Just in case... use the following to avoid path issues
<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.debug.js") %>"
type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js") %>"
type="text/javascript"></script>
Source: http://msdn.microsoft.com/en-us/library/dd381533.aspx
Thanks,
Arty

Load the page in Firefox, then use Firebug to inspect the page - you will be able to see all the individual scripts that have been loaded, plus all the network requests that were issued, and whether they succeeded or not. This is better than trying to troubleshoot from the server perspective.
If you are using IE8, you can use the Developer Tools window, but I think Firebug is better - both tools support JavaScript debugging though.
The most likely problem is that you are running script in your partial view before the scripts it is dependent on have had a chance to load - make sure that any script calls you have inside your partial view will only run once the page has loaded, and not immediately during loading.

All the above cases are ok.But sometimes developer forget to add javascript files for ajax .So please check that also.

Basically you might be missing: MicrosoftMvcAjax., MicrosoftMvcValidation.debug and MicrosoftMvcValidation JS file references.
Do the below steps:
PM> Install-Package MicrosoftAjax
PM> Install-Package MicrosoftMvcAjax.Mvc5
Include them in bundleconfig like below:
bundles.Add(new ScriptBundle("~/bundles/mvcFoolProof")
.Include("~/Scripts/MicrosoftAjax*",
"~/Scripts/MicrosoftMvcAjax*",
"~/Scripts/MicrosoftMvcValidation*",
"~/Scripts/mvcfoolproof*",
"~/Scripts/MvcFoolproofJQueryValidation*",
"~/Scripts/MvcFoolproofValidation*"));
Now it should work without any errors.

Add to web.cofig in section:
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

Regarding your response to Sam, one thing I've noticed in a lot of MVC apps is that people don't know how to deal with the ambiguity of relative paths and the application/runtime. For example, the URL rewriting pretty much guarantees that a particular page can appear at different depths than you anticipated, so ../../images will point somewhere else depending on whether you're looking at /products/widget or /products/widget/12345, even though the view might be the same. As Arty pointed out, the best way to deal with this is to let the engine do the work for you by using a URL utility and application-relative paths that will be fixed up by the application regardless of the context.

I have also found that using the following works with ASP.NET MVC2.
Instead of using MicrosoftMvcAjax.js, you use MicrosoftMvcValidation.js
Hope this helps someone.

Related

VS 2017 Browser Link Not Working with Static HTML

In an ASP.NET 4.6 project I have followed the instructions for enabling Browser Link against a static HTML file (used to bootstrap angular) by entering the following in the proper place in web.config.
<add name="Browser Link for HTML" path="*.html" verb="*"
type="System.Web.StaticFileHandler, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
resourceType="File" preCondition="integratedMode" />
This is a very simple web.config and I am fairly certain it is not an issue of the handler not being added properly as described here.
However when looking at the html source in the browser, it does not contain the script tags for Browser Link described in this article.
I was under the impression that if there was a problem while loading handlers, the application would fail to load.Is there anyway to troubleshoot handlers being loaded in ASP.NET?
I am developing an Angular module for a CMS, so I was not including the <body> tag in my index.html.
It turns out, the <body> tag must be present. I am guessing the handler is using XPATH to insert the Browser Link script tags.

ASP.NET Charting Control only working on some servers

Yesterday we pushed out some changes including the addition of ASP.NET MVC 3 (routing, configuration, etc.) and our ASP.NET charting controls stopped working. In our development environment the ASP.NET chart control works as expected. However in our test and production env. we are getting errors from the DefaultControllerFactory as no path can be found for the axd url - "Pages/Secured/ChartImg.axd"
We've tried comparing any differences with the IIS configuration and web.config and made sure the system.webserver node has the correct handler as so...
<add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
This has not helped and we are not sure what else could be wrong. It appears to be the chart handler is not setup correctly, but we have no way of knowing why.
Any help would be greatly appreciated.
It turns out we had a compiler directive on the user control that switched the ImageStorageMode property of the charting control based on debug/release builds. This explained why it was only working on our dev environments that got a debug build and not the other environments. After discovering that we used the fix from this question ASP.NET Charting Control no longer working with .NET 4, which fixed the routing issue we had for the handler when the ImageStorageMode was set to UseHttpHandler.

Webservice won't accept JSON requests

The main issue is that I cannot run a webservice that accepts requests in JSON format. I keep getting a 500 Server error stating that the "request format is invalid." My ASP.NET AJAX extensions are installed. My server is running Plesk Control Panel 8.6 which is undoubtedly causing these problems.
The default handler for this specified extension is shown in the web.config like so:
For my applications webservice to handle JSON it needs to have this reference:
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
Plesk is not allowing the request to be handled properly. I need to know the correct http directive(s) to put into the web.config to properly handle JSON webservices. I tried posting to the Plesk forum two days ago but no response yet.
Any insight would be great =)
There are confirmed errors with webservices and httphandlers. To avoid grief, upgrade to > Plesk 9.0.

How to use chart control's assemblies in application without installing it?

I am using a web application in which there is a chart control. I had installed the Microsoft chart control exe and Visual studio add on.
The problem is that when I upload the application on the server, there is a problem with the two assemblies missing System.Web.DataVisualization.Design.dll and System.Web.DataVisualization.dll.
So, I want to uninstall the Microsoft chart control exe & Visual studio add on, and instead only use the DLL in the application by giving reference in it.
Now, how do I define chart control on an ASPX page? How do I register the assemblies on the ASPX page and how can I get the chart control from it?
I think you can just add the DLL's to your Bin directory of your website.
Installing the chart controls locally simply adds the DLL's to your computer somewhere (I think). And I believe that the Add-In is to give you support for design mode. It doesn't install anything specific to your projects.
However, you may need to register the controls either on the page or in the web.config. For example, here is the registration of the AjaxControlToolkit controls in the web.config:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<pages>
<controls>
<add tagPrefix="ajaxToolkit" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>
</controls>
</pages>
</system.web>
</configuration>
I have solved the problem.
First, add the library System.Web.DataVisualization.dll as reference.
Then, add the following line in web.config, inside <httpHandlers>
<add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
You may see DataVisualization.Charting.ChartHttpHandler in red, it will build and run successfully. This may be a ReSharper bug.

Registering User Controls in web.config Shows Error, But Controls Work Anyway

This has been bugging me all morning and I can't even figure out where the error is.
In the current web site I am developing (it's not a web application, in case it makes a difference, there are user controls declared as follows:
<controls>
<add tagPrefix="uc1" tagName="TransitLinkAdmin" src="~\controls\TransitLinkAdmin.ascx"/>
<add tagPrefix="uc1" tagName="TransitLinkList" src="~\controls\TransitLinkList.ascx"/>
<add tagPrefix="uc1" tagName="WelcomeMessageAdmin" src="~\controls\WelcomeMessageAdmin.ascx"/>
<add tagPrefix="uc1" tagName="WelcomeMessageDisplay" src="~\controls\WelcomeMessageDisplay.ascx"/>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
So far so good, right?
But when I try to add one of these controls to a page, I get an error telling me that the control cannot be found. So why when I run the page, I can use the control?
The designer is telling me that it cannot find the user control file and it is using the path from the web.config file, so it must be looking at it in some fashion.
If I register the control directly in the page, no problems there.
My assumption (and we now how that works) is that there is some compilation error for the site as a whole that is preventing the intellisense from working.
Thanks in advance, all.
Just a complete guess, but maybe it is because you are using backslashes instead of forward slashes? Try:
<add tagPrefix="uc1" tagName="TransitLinkAdmin" src="~/controls/TransitLinkAdmin.ascx"/>
<add tagPrefix="uc1" tagName="TransitLinkList" src="~/controls/TransitLinkList.ascx"/>
<add tagPrefix="uc1" tagName="WelcomeMessageAdmin" src="~/controls/WelcomeMessageAdmin.ascx"/>
<add tagPrefix="uc1" tagName="WelcomeMessageDisplay" src="~/controls/WelcomeMessageDisplay.ascx"/>
Are you using resharper?
I am not sure what version, but somewhere along the way this artifact had happened.
try updating to the last version.
btw they have an early access program where you can download builds (decent quality), and be updatd with the latest features and fixes.
link to early access program
Close visual studio
Move to the location:
In Windows 7:
C:\Users\Pavel\AppData\Roaming\Microsoft\VisualStudio\10.0\ReflectedSchemas
In Windows XP:
C:\Documents and Settings[your username]\Application Data\Microsoft\VisualStudio\10.0\ReflectedSchemas in windows XP
Delete all the files in that folder location. Note that it is completely safe to delete files in this location.
Then reopen visual studio.
This should solve your problem.

Resources