小编joh*_*ith的帖子

IIS 10上的Web API(处理程序“ ExtensionlessUrlHandler-Integrated-4.0”具有错误的模块“ ManagedPipelineHandler”)

我构建了一个基本的RESTful Web服务,以便可以接收事件。我可以在本地IIS上很好地运行,但是在服务器上的IIS 10中发布它时出现以下错误

HTTP错误500.21-内部服务器错误处理程序“ ExtensionlessUrlHandler-Integrated-4.0”具有错误的模块“ ManagedPipelineHandler”

我在应用程序功能中将应用程序池的托管管道模式设置为集成并启用目录浏览。我的网络配置如下所示:

<system.webServer>
<modules>
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Run Code Online (Sandbox Code Playgroud)

如果我将应用程序池的托管管道模式更改为“经典”,则实际上可以看到根站点,但是尝试浏览或调用API会得到404(例如xx.x.xxx.x / api / Controller / Action)。

c# asp.net iis asp.net-web-api

3
推荐指数
1
解决办法
5875
查看次数

用于单元测试的Mock HttpActionContext

我有一个自定义授权属性如下所示,我正在尝试编写单元测试来测试其功能.

    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        if (actionContext.Request.Headers.Authorization != null)
        {
            // get the Authorization header value from the request and base64 decode it
            string userInfo = Encoding.Default.GetString(Convert.FromBase64String(actionContext.Request.Headers.Authorization.Parameter));

            // custom authentication logic
            if (string.Equals(userInfo, string.Format("{0}:{1}", "user", "pass")))
            {
                IsAuthorized(actionContext);
            }
            else
            {
                HandleUnauthorizedRequest(actionContext);
            }
        }
        else
        {
            HandleUnauthorizedRequest(actionContext);
        }
    }

    protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized)
        {
            ReasonPhrase = "Unauthorized"
        };
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我尝试测试时,我得到"System.NullReferenceException:对象引用没有设置为对象的实例." 我试图设置actionContext的request.headers.authorization值,但它没有setter.当我尝试模拟HttpActionContext时,它说它无法从模拟HttpActionContext转换为真实的HttpActionContext.下面是我的测试代码

public class HttpBasicAuthorizeAttributeTest
{
    private HttpBasicAuthorizeAttribute ClassUnderTest { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq vs-unit-testing-framework

2
推荐指数
1
解决办法
4567
查看次数