简体   繁体   English

在 ASP.NET Core web 应用中设置路由

[英]Setting Routes in ASP.NET Core web application

I have this folder structure in my ASP.NET Core application:我的 ASP.NET Core 应用程序中有这个文件夹结构:

在此处输入图像描述

I am trying to set my routes using the following code:我正在尝试使用以下代码设置我的路线:

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute("Default", "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapGet("/", context =>
        {
            return Task.Run(() => context.Response.Redirect("/Home/Index"));
        });
    });

My default route is /Home/Index but if the user is not authorized then I want to redirect him/her to /Login and not /Home/Login , the current code redirects to /Home/Login , which doesn't exist!我的默认路由是/Home/Index但如果用户未被授权,那么我想将他/她重定向到/Login不是/Home/Login ,当前代码重定向到/Home/Login ,它不存在!

What changes should I make in my code?我应该对我的代码进行哪些更改?

Everything is very strange, but it seems to me you can make it using attribute routing一切都很奇怪,但在我看来你可以使用属性路由来实现

[Route("~/Login")]
[Route("~/Home/Login")]
public IActionResult Login()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET Core 3 MVC Web 应用程序中设置路由 - Setting up routes in ASP.NET Core 3 MVC web app ASP.Net Core使用Web API进行路由 - ASP.Net Core routes with Web API ASP.NET Web API 2设置控制器路由 - ASP.NET Web API 2 Setting Controller Routes 未找到返回 404 的 Asp.Net Core“Web API”路由 - Asp.Net Core "Web API" Routes Returning 404 Not Found 是否可以在 asp.net 核心路由中包含应用程序名称? - Is it possible to include the application name in the asp.net core routes? 缺少“ASP.NET Core Web 应用程序(.NET Core)”模板 - missing “ASP.NET Core Web Application (.NET Core)” template 如何在ASP.NET Core 6.0 Web API中读取Azure App服务的应用设置 - How to read application setting of Azure App service in ASP.NET Core 6.0 Web API 处理身份验证/授权:ASP.NET Core Web 应用程序 => ASP.NET Core Web API => SQL - Handling authentication/authorization: ASP.NET Core Web Application => ASP.NET Core Web API => SQL asp.net 内核中的控制台应用程序和 Web 应用程序之间的区别 - Difference between a console application and Web application in asp.net core 设置asp.net核心MVC应用程序的默认页面 - Setting default page for asp.net core MVC application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM