第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

.Net Core 2 和 AngularJS SPA 不工作

.Net Core 2 和 AngularJS SPA 不工作

C#
拉風(fēng)的咖菲貓 2021-11-21 14:55:00
我一直在將一個(gè)項(xiàng)目從 .NET Framework 遷移到 .NET Core 2,這個(gè)過程比預(yù)期的要順利,但是當(dāng)我認(rèn)為我已經(jīng)克服了最困難的挑戰(zhàn)時(shí),我終生無法獲得 SPA 方面上班。當(dāng)我啟動(dòng)應(yīng)用程序時(shí),只要我轉(zhuǎn)到根目錄 ( https://localhost:5001 ) ,它就可以正常工作,并且我可以使用 Angular 路由進(jìn)行導(dǎo)航。但是,如果我嘗試通過路由加載頁面,例如。https://localhost:5001/login,我得到一個(gè)通用的Chrome 404“找不到本地主機(jī)頁面”。我index.html在目錄中MyApp/Pages/,我的客戶端代碼在MyApp/wwwroot/app/. 當(dāng)移動(dòng)index.html到wwwroot文件夾時(shí),我什么也得不到。在 My 中Startup.cs,這是我的設(shè)置方式:public class Startup{    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.    public void Configure(IApplicationBuilder app, IHostingEnvironment env)    {        if (env.IsDevelopment())        {            app.UseDeveloperExceptionPage();        }        else        {            app.UseExceptionHandler("/Error");            app.UseHsts();        }         app.UseHttpsRedirection();        app.UseStaticFiles();        var defaultFileOptions = new DefaultFilesOptions();        defaultFileOptions.DefaultFileNames.Clear();        defaultFileOptions.DefaultFileNames.Add("/index.html");        app.UseDefaultFiles(defaultFileOptions);        app.Use(async (context, next) =>        {            await next();            if (context.Response.StatusCode == 404 &&                !Path.HasExtension(context.Request.Path.Value))            {                context.Request.Path = "/index.html";                context.Response.StatusCode = 200;                await next();            }        });        app.UseCookiePolicy();        app.UseAuthentication();        app.UseMvc();    }}我嘗試了許多指南,但似乎我能找到的大多數(shù)指南都非常過時(shí),或者在嘗試使用帖子中的方法時(shí),它們是我沒有的方法。非常感謝任何幫助,因?yàn)槲液芟胪瓿蛇w移并繼續(xù)從事該項(xiàng)目。
查看完整描述

3 回答

?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊

這里的問題是你有一個(gè) SPA,而 Angular 有它自己的路由,但是當(dāng)你嘗試使用像https://localhost:5001/login這樣的 URL 時(shí),這條路由是由 MVC 而不是由 Angular 提供的,MVC 會(huì)找不到合適的控制器,結(jié)果返回 404。

你應(yīng)該做的是返回 index.html 以響應(yīng)此類請求,以便 Angular 路由可以處理它。你可以看看這篇文章:

https://www.toptal.com/angular/angular-5-asp-net-core


查看完整回答
反對 回復(fù) 2021-11-21
?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊

原來我的問題出在我處理 404 的方式上。我context.Request.Path = "/index.html";在 Startup.cs中做的,但它仍然無法解析 html 文件,所以我讓它重定向回根路徑:


    app.Use(async (context, next) =>

    {

        await next();

        if (context.Response.StatusCode == 404 &&

            !Path.HasExtension(context.Request.Path.Value))

        {

            // This is what I changed. I need to resolve to "/" instead of "/index.html"

            context.Request.Path = "/";

            context.Response.StatusCode = 200;

            await next();

        }

    });


查看完整回答
反對 回復(fù) 2021-11-21
?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

使用此代碼:


public void Configure(IApplicationBuilder app, IHostingEnvironment env)

{

if (env.IsDevelopment())

{

    app.UseDeveloperExceptionPage();

    app.UseBrowserLink();

}

else

{

    app.UseExceptionHandler("/Error");

}


app.UseStaticFiles();


app.UseMvc(routes =>

{

    routes.MapRoute(

        name: "default",

        template: "{controller}/{action=Index}/{id?}");

});

}


查看完整回答
反對 回復(fù) 2021-11-21
  • 3 回答
  • 0 關(guān)注
  • 213 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號