简体   繁体   English

如何配置IIS以允许AngularJS路由到HTML

[英]How to Configure IIS to allow AngularJS to route to HTML

How do configure my IIS to allow AngularJS to route to an HTML page in my solution? 如何在我的解决方案中配置IIS以允许AngularJS路由到HTML页面?

Here is my routeProvider: 这是我的routeProvider:

$routeProvider.when('/', {
    controller: "ProfileController",
    templateUrl: "/Views/Profile/Main.html"
})

But as expected I receive a 404 message, and I understand that the default configuration will attempt to follow the MVC Net Routing. 但是正如预期的那样,我收到404消息,并且我了解默认配置将尝试遵循MVC网络路由。 How would I achieve this? 我将如何实现? Is there a better solution? 有更好的解决方案吗? I have an idea that I have to change my Web.config file and I think I have to change my <system.webServer> configuration, but I dont know what to change it to. 我有一个想法,我必须更改Web.config文件,并且我想必须更改<system.webServer>配置,但是我不知道将其更改为什么。

Here is my current(default) <system.webServer> configuration: 这是我当前的(默认) <system.webServer>配置:

<system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </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>
  </system.webServer>

EDIT: 编辑:
The reason I bring up the IIS is because I have read on several sites to include something like following to the </system.webServer> configuration link : 我提出IIS的原因是因为我已经在几个站点上阅读了类似</system.webServer>配置链接的内容

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
  <rewrite>
    <rules>
      <!--Redirect selected traffic to index -->
      <rule name="Index Rule" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
        </conditions>
        <action type="Rewrite" url="/index.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

There is nothing wrong with IIS. IIS没有错。 By default an MVC application is configured to not return anything residing in the Views folder. 默认情况下,MVC应用程序配置为不返回Views文件夹中的任何内容。

My recommendation would be to put your static HTML file(s) in a folder other than Views. 我的建议是将您的静态HTML文件放在Views以外的文件夹中。

If for some reason you can't do that, go to the Views folder and open the web.config. 如果由于某种原因您不能执行此操作,请转到“视图”文件夹并打开web.config。 You'll find something like the following. 您会发现类似以下内容。

<handlers>
  <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>

Basically that's saying that no matter what is asked for using any HTTP verb you're going to get a 404. You can change the path="*" to something more specific, such as path="*.cshtml" which would prevent your Razor views from being served up directly but allow other files to still be served. 基本上,这就是说,无论使用任何HTTP动词要求什么,您都将获得404。您可以将path="*"更改为更具体的内容,例如path="*.cshtml" ,这会阻止您剃刀视图无法直接提供,但允许其他文件继续提供。

I see you're using .html and not .cshtml. 我看到您使用的是.html,而不是.cshtml。 I've recently tried this with ASP.NET MVC and noticed one option is to have your first page named index.html and in the root of or project and remove all MVC routing stuff. 我最近在ASP.NET MVC中进行了尝试,并注意到一个选择是将您的第一页命名为index.html,并在或项目的根目录中,并删除所有MVC路由内容。 IIS by default will see that index.html is the only option it has and run that page. IIS默认情况下将看到index.html是它唯一的选项并运行该页面。 Then in your angular routing you can put the other pages in Views and route to that. 然后,在角度路由中,您可以将其他页面放在“视图”中并路由到该页面。

The biggest issue, and in my mind a big one, is that to be able to get pretty url direct navigation (the user types yoururl.com/help) you have to do the last part you mentioned. 最大的问题(在我看来是个大问题)是,要获得漂亮的url直接导航(用户键入yoururl.com/help),您必须做最后提到的部分。 You have to do this because your routing is done in index.html and that allows the pretty url to work. 您必须这样做,因为路由是在index.html中完成的,并且允许漂亮的url起作用。 The user can use the # sign's in the url and get there but that sucks for them. 用户可以使用url中的#号到达那里,但这很麻烦。

It just seems lame that we have to change the web server config to account for all of this. 似乎很la脚,我们必须更改Web服务器配置以解决所有这些问题。 I get why, but sucks we have to do it and there wasn't some other easier way. 我知道为什么,但是很糟糕,我们必须这样做,没有其他更简单的方法了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM