Bootstrap5 和其他自定义 CSS 未加载到 .net 核心 Web api 中 [英] Bootstrap5 and other custom CSS not loaded in .net core web api

查看:13
本文介绍了Bootstrap5 和其他自定义 CSS 未加载到 .net 核心 Web api 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,目前我正在开发一个 api 项目,该项目在本地呈现 cshtml 模板以用于报告目的.问题是,当使用 bootstrap 的 cdn 链接时,cshtml 会以 acordinly 呈现,但是当在本地使用静态 css 文件时,所有样式都没有应用.我在网上查看,发现您需要在 startup.cs 中使用静态文件中间件,但它也不起作用.

Hi currently I am on a project of an api that render a cshtml template localy for reporting purpose. The issue is that when using the cdn link of bootstrap the cshtml is rendered acordinly but when using static css file locally all the style is not applied. I have looked on the web and found that you need to use static file middleware in the startup.cs but its not working either.

这是我的目录的屏幕截图:

Here is the screenshoot of my directory:

这里是我在configure方法中添加的代码:

and here is the code I added in the configure method:

app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles")),
            RequestPath = "/StaticFiles"
        });

这里是在 cshtml 文件中加载 css 的代码示例:

and here is a sample of the code loading the css in cshtml file:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>@Model.title</title>
    @*<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>*@
    <link href="grafana.light.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="bootstrap.min.css"/>
</head>

有没有人解决这个问题?亲切的问候

Does anyone have a fix for this? Kind Regards

更新:

Directory.GetCurrentDirectory() return the folowing path:

andh 项目所在的文件夹:

andh the folder where the project is located:

出于保密目的,我不得不掩盖文件夹的名称,但我可以证明它是同一个文件夹.

For confidentiality purpose I had to mask the name of the folder but I can testify its the same folder.

推荐答案

我将把这个设为关闭,因为我认为问题与静态文件无关,而是来自我得到的生成的 html.相反,我将发布另一个问题,询问如何在启用 css 和静态文件的情况下从 cshtml 呈现 html 文件.新问题

I will put this one as closed cause I think the problem is not related to the static file but from the generated html i got. Instead i will post another question asking how to render html file from cshtml with css and static file enabled.new question

这篇关于Bootstrap5 和其他自定义 CSS 未加载到 .net 核心 Web api 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆