从.NET Core Web应用程序中未正确加载清单。

huangapple go评论67阅读模式
英文:

Manifest not loaded correctly from .NET Core Web Application

问题

只是为了从技术角度看看可能性,我试图将一个.NET Core Razor Pages的Web应用程序(没有MVC!)转换为渐进式Web应用程序(PWA),但我遇到了问题。

我的浏览器不想正确加载我创建的清单。在Chrome DevTools中,我收到了一条消息,说第1行第1列存在语法错误,但当我单击Web清单的链接时,它说"未找到具有给定URL的资源"。这让我得出结论,它只是拒绝加载我的清单。

我遵循了一个教程(他们使用了MVC Web应用程序,而我没有),并安装了NuGet包'WebEssentials.AspNetCore.PWA',并将以下行添加到我的Program.cs文件中:

builder.Services.AddProgressiveWebApp();

这使它完整如下:

var builder = WebApplication.CreateBuilder(args);

// 将服务添加到容器中。
builder.Services.AddRazorPages();

var app = builder.Build();

// 配置HTTP请求管道。
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // 默认的HSTS值为30天。您可能希望针对生产方案进行更改,参见https://aka.ms/aspnetcore-hsts。
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

然后,我在wwwroot文件夹中创建了一个manifest.json文件,内容如下:

{
    "name": "Razor PWA",
    "short_name": "Razor PWA",
    "icons": [
        {
            "sizes": "192x192",
            "type": "image/png",
            "src": "/img/icon-192.png"
        },
        {
            "sizes": "512x512",
            "type": "image/png",
            "src": "/img/icon-512.png"
        }
    ],

    "display": "minimal-ui",
    "theme_color": "#2dab66",
    "orientation": "portrait",
    "background_color": "#2dab66",
    "start_url": "/"
}

我还将图标添加到与manifest.json位于相同位置的img文件夹中。

我还尝试使用MVC项目执行完全相同的过程,结果非常顺利。因此,我相信MVC和非MVC之间存在差异,阻止了相同的过程起作用,我想知道是否需要更改任何配置,或者也许无法使用此包将非MVC应用程序转换为PWA?

我会接受任何答案!

英文:

Just to try and see what's possible from a technical point of view, I am trying to make a .NET Core Web Application with Razor Pages (NO MVC!) into a Progressive Web App (PWA) but I am running into a problem here.

My browser does not want to load the manifest that I created correctly. In Chrome DevTools I get this message that there is a syntax error on line 1 column 1 but when I click the link to the webmanifest, it says that "No resource with given URL found". That brings me to the conclusion that it is just refusing to load my manifest.

I followed a tutorial (where they were using an MVC web app, I'm not) and installed the NuGet package 'WebEssentials.AspNetCore.PWA' and added the following line to my Program.cs file:

builder.Services.AddProgressiveWebApp();

which makes it look like this in full:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

Then I created a manifest.json file in the wwwroot folder with the following content:

{
    "name": "Razor PWA",
    "short_name": "Razor PWA",
    "icons": [
        {
            "sizes": "192x192",
            "type": "image/png",
            "src": "/img/icon-192.png"
        },
        {
            "sizes": "512x512",
            "type": "image/png",
            "src": "/img/icon-512.png"
        }
    ],

    "display": "minimal-ui",
    "theme_color": "#2dab66",
    "orientation": "portrait",
    "background_color": "#2dab66",
    "start_url": "/"
}

and I also added icons to an img folder at the same location as the manifest.json.

I also tried the exact same process with a MVC project and it worked like a charm. So I am convinced that there is a difference between MVC and non-MVC that stops this same process from working and I'm wondering if there is any configuration I need to change, or maybe it isn't even possible to make a non-MVC application into a PWA with this package?

I would be happy with any answers!

答案1

得分: 0

> WebEssentials.AspNetCore.PWA

这个包似乎相当古老。如果你安装它,它会引入所有的 ASP.NET Core 2。考虑使用另一种方法。

不管怎样,你打算使用的库将公开一个 /manifest.webmanifest 端点,通过它返回转换后的 manifest.json,但正如你注意到的那样,这个URL返回404。

这个路由没有匹配到,因为该库在 一个控制器 中实现它,但你只路由到 Razor 页面。

所以,请将你的启动更改为同时路由到默认的控制器约定:

app.MapRazorPages();
app.MapDefaultControllerRoute();
英文:

> WebEssentials.AspNetCore.PWA

It looks like the package is quite ancient. If you install it, it pulls in all of ASP.NET Core 2. Consider using another approach.

Anyhow, the library you intend to use will expose a /manifest.webmanifest endpoint, through which it will return the transformed manifest.json, but as you noticed, this URL returns a 404.

This route isn't matched, because the library implements it in a controller, but you only route to Razor pages.

So change your startup to also route to the default controller convention:

app.MapRazorPages();
app.MapDefaultControllerRoute();

huangapple
  • 本文由 发表于 2023年3月3日 18:21:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定