InvalidOperationException thrown regarding DotNetOpenAuth.IEmbeddedResourceRetrieval with Razor view

后端 未结 2 1360
小蘑菇
小蘑菇 2021-01-02 18:54

When my Razor view calls @Html.OpenIdSelector(... I get an InvalidOperationException:

The current IHttpHandler is not one of

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-02 19:14

    I came across this problem when I was trying to get the NerdDinner OpenID working for MVC3/razor. The solution that has worked for me is:

    Create this class in your solution: (this was found in one of the NerdDinner builds - http://nerddinner.codeplex.com/SourceControl/changeset/view/55257#1328982 (thanks to JasonHaley) for some reason, this file isn't in the latest build for NerdDinner MVC3/Razor

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using DotNetOpenAuth;
    namespace .Services
    {
        public class EmbeddedResourceUrlService : IEmbeddedResourceRetrieval
        {
    
            private static string pathFormat = "{0}/{1}/Resource/GetWebResourceUrl?assemblyName={2}&typeName={3}&resourceName={‌​4}";
            //private static string pathFormat = "{0}/Resource/GetWebResourceUrl";
    
            public Uri GetWebResourceUrl(Type someTypeInResourceAssembly, string manifestResourceName)
            {
                if (manifestResourceName.Contains("http"))
                {
                    return new Uri(manifestResourceName);
                }
                else
                {
                    var assembly = someTypeInResourceAssembly.Assembly;
    
                    // HACK
                    string completeUrl = HttpContext.Current.Request.Url.ToString();
                    string host = completeUrl.Substring(0,
                        completeUrl.IndexOf(HttpContext.Current.Request.Url.AbsolutePath));
    
                    var path = string.Format(pathFormat,
                                host,
                                HttpContext.Current.Request.ApplicationPath.Substring(1),
                                HttpUtility.UrlEncode(assembly.FullName),
                                HttpUtility.UrlEncode(someTypeInResourceAssembly.ToString()),
                                HttpUtility.UrlEncode(manifestResourceName));
    
                    return new Uri(path);
                }
            }
        }
    }
    

    Then in your web.config, add this line:

    
    

    just put in the DotNetOpenAuth section

    you will also need this route set up: ( Html.OpenIdSelectorScripts helper method throwing NullReferenceException )

        routes.MapRoute(
            "OpenIdDiscover",
            "Auth/Discover"
        );
    

提交回复
热议问题