在Web配置中添加<customheaders>将导致错误500内部服务器错误</customheaders>

时间:2014-12-08 15:25:30

标签: asp.net-mvc asp.net-mvc-3 iis-7.5 cors

我的网站是由ASP.NET MVC 3编写的,并且在IIS 7.5上托管以解决CORS问题,我需要添加自定义标题。

我按照此链接中的步骤http://msdn.microsoft.com/en-us/library/bb763179%28v=vs.100%29.aspx但是将这些行添加到Web.Config文件将导致以下错误

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="CustomHeader" value="CustomHeader" />
      <customHeaders>
    </httpProtocol>
  </system.webServer>

错误

500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.

由于我的网络配置中已存在<system.webServer>标记,因此我尝试将内容添加到现有标记并单独添加,但两者都无效。

任何帮助将不胜感激。

的Web.Config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>

    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
      <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="AgentLog.log" formatter="Text Formatter" header="" footer="" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Increment" rollSizeKB="10000" traceOutputOptions="DateTime, Timestamp"  maxArchivedFiles="10"/>
    </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="[{category}][{timestamp}]: {message}{newline}" name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Rolling Flat File Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="Rolling Flat File Trace Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
  <connectionStrings>
    <add name="DinnerConnectionString" connectionString="xxx"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
<appSettings configSource="User.Config"/>
  <system.web>

    <httpHandlers>
       <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          validate="false" />
    </httpHandlers>
    <customErrors mode="Off" >
    </customErrors>
    <!--<customErrors mode="Off" defaultRedirect="Error" >
            <error statusCode="404" redirect="Error/Error404" />
            <error statusCode="500" redirect="Error/Error500" />
        </customErrors>-->
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </assemblies>
    </compilation>
    <authentication mode="Forms">
            <forms loginUrl="~/Account/LogOn" timeout="43200" slidingExpiration="true" name="ShamAuth" protection="All"  />
    </authentication>
    <sessionState timeout="1440"></sessionState>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages"/>
    <add namespace="MvcContrib.UI.Grid.ActionSyntax" />
      </namespaces>
   <controls>
    <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting"
     assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
   </controls>
    </pages>
        <caching>
            <outputCacheSettings>
                <outputCacheProfiles>
                    <add name="ZeroCacheProfile" duration="0" varyByParam="None" location="None" />
                </outputCacheProfiles>
            </outputCacheSettings>
        </caching>
  </system.web>

  <!--<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50000000"/>
      </webServices>
    </scripting>
  </system.web.extensions>-->
    <location path="Scripts">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>
    <location path="Contents">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>
    <location path="Images">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  <handlers>
   <remove name="ChartImageHandler" />
   <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" />
  </handlers>

<httpProtocol>
  <customHeaders>
    <add name="CustomHeader" value="CustomHeader" />
  <customHeaders>
</httpProtocol>

  </system.webServer>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

2 个答案:

答案 0 :(得分:1)

我找到了解决方案,

在IIS中,我删除了X-Powered-By标题,现在一切正常。

enter image description here

enter image description here

答案 1 :(得分:0)

在配置文件的自定义标头部分下使用以下标记

$.ajax({
            url: '/setSession',
            type: 'POST',
            Accept : 'application/json',
            //contentType: 'application/json',
            dataType: 'json',
            data: { "hello": templateName}, 
            //data: JSON.stringify({ "hello": templateName}), 
            headers: {
               'Authorization': 'Bearer '+ token
            },
            success: function (result) {console.log('post ok'+JSON.stringify(result));
                // CallBack(result);
            },
            error: function (error) { console.log('post failed'+JSON.stringify(error));
         
            }
         });