【问题标题】:How to add rules to .net core project in *.csproj如何在 *.csproj 中向 .net 核心项目添加规则
【发布时间】:2020-10-07 07:36:24
【问题描述】:

我想向 .net 核心项目添加规则。 在 .net 普通项目中,我可以直接编辑 web.config 文件

实际上我需要在 .net 核心项目中实现这一点

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\RazorWeb.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="OutOfProcess" />
    </system.webServer>
  </location>
  <system.webServer>
    <rewrite>
     <!---START:ADD THIS RULES-->
      <rules>
        <rule name="httpTohttps" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
      <!---END:ADD THIS RULES-->
    </rewrite>
  </system.webServer>
</configuration>

好吧,自从我在 .net core 中引入之后,我注意到模板项目没有附带这个 .config 文件,但是模板使用 .csproj 文件和 .json 文件来添加配置等...

主要思想是如何在.net核心项目中实现这一点,我在不同的站点中搜索,微软的文档在这方面对我来说并不完全清楚,我找到的最接近的就是这个

https://roslyn-analyzers.readthedocs.io/en/latest/config-analyzer.html 但对我不起作用

【问题讨论】:

    标签: c# web .net-core web-config csproj


    【解决方案1】:

    看到这个。您应该使用中间件而不是 web.config 来强制执行 https。 https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-3.1&tabs=visual-studio

    您仍然可以在需要时在 .NET Core 项目中包含 web.config 文件。事实上,当您点击 Publish 并且还没有一个时会生成一个,这可以为您提供一个创建自定义的起点。

    执行以下操作之一:

    1. 在添加项目中添加“Web 配置”模板,就像这个答案很好地包含屏幕截图:https://stackoverflow.com/a/53731666/691749
    2. 改为创建您自己的 web.config,并将其设置为始终复制到输出目录。这将替换 Publish 上自动生成的内容。

    关于这个文件有很多令人困惑的信息。这是一个体面的问题,有体面的答案。 Am confused about web.config in .NET Core

    【讨论】:

    • 你好,谢谢,是的,我一直看到.NET Core 仍然支持 web.confing 文件,比强制 https 是添加重定向规则更重要,我打算对 Web.config 文件执行此操作
    猜你喜欢
    • 2022-06-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2021-07-03
    • 2018-02-17
    • 2018-01-23
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多