c# - 如何在 asp.net 核心 mvc/razor 页面混合应用程序中使默认页面使用 MVC 默认页面,“主页/索引”页面
问题描述
我有一个 asp.net mvc 核心应用程序,我将 MVC 和 Razor Pages 组合在一起。
目前,当我加载应用程序时,它会加载 Index Razor Page 。
我正在尝试制作初始页面(https://localhost:44340/)以从 mvc 呈现https://localhost:44340/home/index视图。
我怎样才能做到这一点?
这是我在 startup.cs 文件中的代码:
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.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
}
// 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("/Home/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.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
家庭控制器:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
解决方案
推荐阅读
- python - 如何编写程序来计算相同元素的数量并将它们连接到一个数据帧中?
- python - 在 Python 中使用 K-means 将地理数据聚类到相同大小的组中
- unreal-engine4 - 有没有办法让物体在两点之间线性移动?
- amazon-web-services - 使用 CodeDeploy 到 ECS 的 AWS CodePipeline 找不到 appspec.yml
- java - 如何在 Java 中将“org.w3c.dom.Document”对象转换为“org.apache.commons.jxpath.xml.DocumentContainer”对象
- android - Android:通过在给定范围内的基础地图卫星视图上检测农场中的路径来绘制多边形
- java - java 模块在实践中是否提供不同的编译和运行时限制?
- android - Android 领域迁移问题 → java.lang.IllegalArgumentException:创建对象后无法更改主键字段“foo”
- python - 自定义表情符号和动画表情符号不起作用 [discord.py-rewrite]
- angular - 如何使用 .NET Core 2 配置 Angular 6 以允许来自任何主机的 CORS?