2016-02-26 45 views
1

我已經徹底研究過這個項目,在我看來,做正確的事情。但是,它不起作用,所以我的意見一定是錯的!IIS重寫規則無法按預期方式工作

我有一個網站,其中所有的內容都在wwwroot或其他文件夾中。 我想讓HTML5網址正常工作。 該網站提供了5種URL類型。

  1. 一個空的URL。
  2. 用於物理文件,例如CSS,HTML或JS文件
  3. 的URL/API的URL/...訪問資源
  4. 的URL /的OAuth /認證
  5. 的HTML5標記URL類型,說/公司簡介

我在web.config中

<rule name="index.html as document root" stopProcessing="true"> 
     <match url="^$" /> 
     <action type="Rewrite" url="/wwwroot/index.html" /> 
    </rule> 
    <rule name="static dist files" stopProcessing="true"> 
     <match url="^(.+)$" /> 
     <conditions> 
     <add input="{APPL_PHYSICAL_PATH}wwwroot\{R:1}" matchType="IsFile" /> 
     </conditions> 
     <action type="Rewrite" url="/wwwroot/{R:1}" /> 
    </rule> 
    <rule name="api call" stopProcessing="true"> 
     <match url="^/api/" /> 
     <action type="None"/> 
    </rule> 
    <rule name="authorization call" stopProcessing="true"> 
     <match url="^/oauth/token$" /> 
     <action type="None"/> 
    </rule> 
    <rule name="reroute html5 to index.html" stopProcessing="true"> 
     <match url="^.+$" /> 
     <action type="Rewrite" url="/wwwroot/index.html" /> 
    </rule> 

以下重寫規則我看不到我在做什麼錯。我能找到的關於「stopProcessing」的文檔是不精確的,但我假設它意味着如果規則匹配,不要處理更多規則。

發生的情況是,沒有調用API或用於身份驗證的正確處理 - 通過調試API不被調用,而是將其重寫爲wwwroot/index.html。

我確信我正在做一些非常簡單的事情,但我已經嘗試過所有我能想到的事情。

任何幫助表示讚賞。

服務器正在使用OWIN和JWT,而前端是AngularJS。我正在使用VS2015社區進行開發。

回答

0
<defaultDocument> 
    <files> 
    <clear /> 
    <!-- This is the root document for the Angular app --> 
    <add value="wwwroot/index.html" /> 
    </files> 
</defaultDocument> 

<rewrite> 
    <rules> 

    <rule name="static dist files" stopProcessing="true"> 
     <match url="^(.+)$" /> 
     <conditions> 
     <add input="{APPL_PHYSICAL_PATH}wwwroot\{R:1}" matchType="IsFile" /> 
     </conditions> 
     <action type="Rewrite" url="/wwwroot/{R:1}" /> 
    </rule> 

    <rule name="Main Rule" stopProcessing="true"> 
     <match url=".*" /> 
     <conditions logicalGrouping="MatchAll"> 
     <!-- Allows "api/" prefixed URLs to still hit Web API controllers 
         as defined in WebApiConfig --> 
     <add input="{REQUEST_URI}" pattern="api/" ignoreCase="true" negate="true" /> 
     <!-- Allow "oauth/token" to do autnentication --> 
     <add input="{REQUEST_URI}" pattern="oauth/token" ignoreCase="true" negate="true"/> 
     </conditions> 
     <action type="Rewrite" url="/" /> 
    </rule> 

    </rules> 
</rewrite> 

經過大量的試驗和錯誤,我終於得到它的工作,現在我有它的工作,這都是顯而易見的。

defaultDocument負責原始問題中的案例1。 「靜態dist文件」規則處理案例2. 「主規則」負責處理案例5,確保案件3和案例4不變。