【发布时间】:2020-07-07 17:28:48
【问题描述】:
我有一个站点,可以路由到各种 blazor 组件页面,这些页面在我的本地计算机上都可以正常工作。然而,在部署到 iis 后,其中一些不再工作。
/
/request
但是带有参数的路由都失败并返回 4040
/request/1
/edit/1
有人可以帮忙吗?我什至不知道从哪里开始寻找。
更新: 所以我最初的假设是错误的。问题不是路线的格式,而是其他问题,我怀疑这很容易解决。你们中的大多数人都会为我的特殊性而叹息。
它是 Blazor 服务器托管的应用程序。
启动
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PartCreation.Data;
using PartCreation.Interfaces;
using PartCreation.Models;
using PartCreation.Repositorys;
namespace PartCreation
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
var emailConfig = Configuration
.GetSection("EmailConfiguration")
.Get<EmailConfiguration>();
services.AddSingleton(emailConfig);
services.AddHttpClient("HttpClientWithSSLUntrusted").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
ClientCertificateOptions = ClientCertificateOption.Manual,
ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) =>
{
return true;
}
});
services.AddScoped<IEmail, EmailSenderRepository>();
services.AddScoped<IBrands, BrandsRepository>();
services.AddScoped<IUom, UomRepository>();
services.AddScoped<ISuppliers, SupplierRepository>();
services.AddScoped<IPartClass, PartClassRepository>();
services.AddScoped<IPartCreation, PartCreationRepository>();
services.AddScoped<IEpicor, EpicorRepository>();
services.AddAutoMapper(new[] { typeof(AutomapperProfiles).Assembly });
services.AddDbContext<ErpDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("ErpConnection")));
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("RequestsConnection")));
services.AddRazorPages();
services.AddServerSideBlazor();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
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.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
导航菜单中的链接有效,但页面链接无效。
<a class="navbar-brand" href="">PartCreation</a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
<ul class="nav flex-column">
<li class="nav-item px-3">
<NavLink class="nav-link" href="request">
<span class="oi oi-list-rich" aria-hidden="true"></span>New Request
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="requests/me" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> My Requests
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Requests
</NavLink>
</li>
</ul>
</div>
@code {
private bool collapseNavMenu = true;
private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
private void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
}
}
上述代码中的所有链接都有效并将链接解析为
- http://servername/partcreation/request
- http://servername/partcreation/requests/me
@page "/"
@page "/requests"
@inject IPartCreation _requests
<div class="m-auto" style="width:90%;">
<table class="table table-striped w-100">
<thead>
<tr>
<th></th>
<th>Id</th>
<th>Priority</th>
<th>Status</th>
<th>Part number</th>
<th>Requester</th>
<th>Requested</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
@if (Requests != null)
{
@foreach (var item in Requests)
{
<tr>
@if (IsAdmin)
{
<td>
<a class="btn btn-success" href="request/@item.Id">Edit</a>
<a class="btn btn-primary" href="requests/me">View</a>
</td>
}
else
{
<td>
<a class="btn btn-primary" href="/request/me">View</a>
</td>
}
<td>@item.Id</td>
<td>@item.Priorty</td>
<td>@item.Status</td>
<td>@item.Partnumber</td>
<td>@item.Requester</td>
<td>@item.Requested</td>
<td>@item.Comments</td>
</tr>
}
}
</tbody>
</table>
<Pagination CurrentPage="@paginationDTO.Page" TotalAmountPages="totalAmountPages" Radius="1"
SelectedPage="SelectedPage" />
</div>
@code {
List<RequestListDto> Requests;
PaginationDto paginationDTO = new PaginationDto() { RecordsPerPage = 10 };
private int totalAmountPages;
bool IsAdmin = false;
protected override async Task OnInitializedAsync()
{
await Admincheck();
await LoadRequests();
}
async Task Admincheck()
{
IsAdmin = await _requests.IsAdmin(Environment.UserName);
}
private async Task LoadRequests()
{
var paginatedResponse = await _requests.GetRequest(paginationDTO);
Requests = paginatedResponse.Response;
totalAmountPages = paginatedResponse.TotalAmountPages;
}
private async Task SelectedPage(int page)
{
paginationDTO.Page = page;
await LoadRequests();
}
}
当我使用 Visual Studio 和 IIS express/ 时,上述代码中的链接有效 但是当我部署到服务器默认网站 >> 部分创建时,上述链接不起作用,它们解析为
- 服务器名称/请求/我
- 服务器名称/请求
所以由于某种原因错过了“partcreation”部分。
如果有区别,导航菜单位于默认共享文件夹中,而其他页面位于默认页面文件夹中
【问题讨论】:
-
Blazor 服务器、独立的 Web 程序集还是 ASP.NET 托管的 Web 程序集?您能否显示您的 Program 和 StartUp 类代码以及页面路由。
-
在 IIS 服务器上,用户对服务器上资源的访问权限有限。您以访客身份连接。因此,我怀疑失败的路由正在尝试使用 GUEST 没有访问凭据的服务器上的资源。 Http 错误 404 表示找不到 URL。
-
@PeterMorris 您好,感谢您的回复。我已经更新了问题。我首先想到的是一个不同的问题,我怀疑解决起来要简单得多,我只是不知道。我之前部署时没有遇到过这个问题。我添加了 href 起作用的 NavMenu 页面,以及 href 不起作用的页面。在我看来,它们似乎是一样的。
-
page "/requests" 只能在没有参数的情况下工作。 page "/requests" page "/requests/{id}" 你需要同时定义
标签: c# parameters routes blazor