Acces-Control-Allow-Origin适用于Web.config(IIS7)但不适用于(WebApiConfig.cs)ASP.NET Cross Origin支持

时间:2017-02-07 20:43:52

标签: asp.net iis-7 cors

对于我想加载和查看带有angular-pdfjs的pdf文件的项目。该团队使用ASP.net Cross Origin,Allow-Acces-Control,Headers,Credentials等。

    public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Replace the default implementation of the ITraceWriter with our custom logger.
        config.Services.Replace(typeof (ITraceWriter), new GlobalTraceLogger());

        // Replace the default exception logger to be able to log exceptions with NLog
        config.Services.Replace(typeof (IExceptionLogger), new GlobalExceptionLogger());

        // Replace the default exceptionhandler to be able to handle exceptions globally
        config.Services.Replace(typeof (IExceptionHandler), new GlobalExceptionHandler());

        // We must enable cors, because otherwise we are not able to commuincate with a java script client
        // TODO: We need to restirct the requested resource. Do not allow every origin!
        //       Do not run this in prodocutive environment

        var cors = new EnableCorsAttribute("*", "*", "*", "*");
        cors.SupportsCredentials = true;
        config.EnableCors(cors);
        config.MapHttpAttributeRoutes();

        // Make the default return type JSON
        var appXmlType =
            config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

到目前为止效果不错,但是如果我想用angular-pdfjs加载我的pdf文件,我会得到一个Cross Origin Error,因为Allow-Acces-Control-Origin" *"我的pdf-url没有用。

https://img3.picload.org/image/roirrgcw/corsworksnot.png

但如果我在Web.config中使用而不是ASP.net Cross Origin支持IIS7的允许访问控制:

<system.webServer>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested- With, Content-Type, Accept" />
    <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
  <add name="Access-Control-Allow-Credentials" value="true" />
  </customHeaders>
</httpProtocol>

它有效,pdf将正确加载。

https://picload.org/image/roirrgci/corsworks.jpg

但问题是,目前页面是通过&#34; file://&#34;加载的。所以我明白了 错误,因为没有Access-Control-Allow-Origin为&#39; null&#39;。这意味着,我的pdf正确加载,但登录,图片......不再被加载。所以我的问题是,如果有人知道如何改变我的pdf文件获得Access-Controll-Allow的WebApiConfig-Implementation。或者也许有人可以告诉错误可能在哪里。

有关信息:

这就是我用angular-pdfjs加载pdf的方式:

<!---------------------------THE PDF VIEWER DIRECTIVE------------------------->

<div pdf-viewer="options" pdf-url="pdfUrl" id="my-viewer" class="col col-lg-10"></div>

<!---------------------------THE PDF VIEWER DIRECTIVE------------------------->

那就是网址,我正在使用:

function PdfviewController(ebmGuideLineService, mediaService, $scope, $window) {
    var vm = this;
    $scope.pdfUrl = 'http://localhost:3787/NCCN_Evidence_Blocks_Melanoma.pdf';
    $scope.options = { mouseZoom: false, mousePan: false };

请告诉我,如果您需要更多信息并感谢您的帮助。

0 个答案:

没有答案