Question

I'm developing a visual web part for Sharepoint 2010. This webpart needs to get some data via AJAX. So I went for the HTTP handler + jQuery solution posted here.

But I'm getting a problem when trying to access the handler. I get the famous .NET error page:

Server Error in '/' Application. Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".

Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.

So I checked the server's Event viewer to see the details of the error. This is what is causing it:

Event code: 3006 
Event message: A parser error has occurred. 
Event time: 8/4/2011 2:08:17 PM 
Event time (UTC): 8/4/2011 6:08:17 PM 
Event ID: 71c80726be98453ab77a7c1d474cbf7c 
Event sequence: 5 
Event occurrence: 2 
Event detail code: 0 

Process information: 
    Process ID: 6200 
    Process name: w3wp.exe 
    Account name: NT AUTHORITY\NETWORK SERVICE 

Exception information: 
    Exception type: HttpParseException 
    Exception message: Could not create type 'MyNamespace.MyHandler'. 

Request information: 
    Request URL: http://mysharepointurl/_layouts/MyNamespace/MyHandler.ashx 
    Request path: /_layouts/MyNamespace/MyHandler.ashx 
    User host address: xxx.xxx.xxx.xxx
    User:  
    Is authenticated: False 
    Authentication Type:  
    Thread account name: NT AUTHORITY\NETWORK SERVICE 

Thread information: 
    Thread ID: 11 
    Thread account name: NT AUTHORITY\NETWORK SERVICE 
    Is impersonating: True 
    Stack trace:    at System.Web.UI.SimpleWebHandlerParser.GetType(String typeName)
   at System.Web.UI.SimpleWebHandlerParser.GetTypeToCache(Assembly builtAssembly)
   at System.Web.Compilation.SimpleHandlerBuildProvider.GetGeneratedType(CompilerResults results)
   at System.Web.Compilation.BuildProvider.CreateBuildResult(CompilerResults results)
   at System.Web.Compilation.BuildProvider.GetBuildResult(CompilerResults results)
   at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.UI.SimpleHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
   at System.Web.UI.SimpleHandlerFactory.GetHandler(HttpContext context, String requestType, String virtualPath, String path)
   at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

This is MyHandler.ashx

<%@ WebHandler Language="C#" Class="MyNamespace.MyHandler" CodeBehind="MyHandler.cs" %>

As you can see I'm missing the Assembly directive. But I don't know what to put in it. Where do I get the KeyToke, Name, etc?

Was it helpful?

Solution

I've ended up openning Visual Studio Command Prompt. Navigating to the bin/Release folder of my Web Part and use the sn -T command on my DLL to get the Token!

OTHER TIPS

In visual studio, learn to love the SharePoint Tokens that are available. Read more about them here: http://www.andrewconnell.com/blog/archive/2009/12/03/sharepoint-2010-dev-tidbit-use-the-tokens-in-visual.aspx

I did the same thing today with a separate .ashx file and a .cs file and it works for me. I got the same error but then noticed I had made a class name mistake. Here is my code:

*****DemoHandler.cs********
using System;
using System.Web;
using Microsoft.SharePoint;

namespace CustomHTTPModule
{
    public class DemoHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }


        public void ProcessRequest(HttpContext context)
        {
            SPSite siteColl = SPContext.Current.Site;
            SPWeb site = SPContext.Current.Web;
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World from " +
                                   site.Title +
                                   " at " +
                                   site.Url);
        }
    }
}


*********DemoHandler.ashx*******************
<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="CustomHTTPModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9eec5e2ad94ff809" %>
<%@ WebHandler Language="C#"  Class="CustomHTTPModule.DemoHandler" CodeBehind="DemoHandler.cs" %>

I chose an empty SharePoint project in VS 2010 Visit my blog below for an image of the project structure

Click deploy it should deploy your CustomHTTPModule dll to GAC and the .ashx to the _Layouts of your web app.If you browse for "http://webappname/sites/siteName/CustomHttpModule/DemoHandler.ashx" should give you the text with the site name I referred to http://blogs.msdn.com/b/kaevans/archive/2010/08/04/deploying-an-asp-net-httphandler-to-sharepoint-2010.aspx

Quick and dirty method? Drop the assembly in the GAC and get properties on it. Once you have what you need, you can right-click uninstall it.

Update(15JAN13):

You can also add an external tool to visual studio to help with this (Tools->External Tools...):

Title: Get Assembly Name
Command: powershell.exe
Arguments: -command  "[System.Reflection.AssemblyName]::GetAssemblyName(\"$(TargetPath)\").FullName"
Use Output Window: checked

When your run the command, the output window will show something like this:

YourAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9999d8e777777f66

You can do the same kind of thing for the strong name tool:

Title: Get Strong Name Token
Command: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sn.exe
Arguments: -T $(TargetPath)
Use Output Window: checked

With the output window showing something like this:

Microsoft (R) .NET Framework Strong Name Utility Version 3.5.30729.1 Copyright (c) Microsoft Corporation. All rights reserved.

Public key token is 9999d8e777777f66

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top