ASP.NET 页面自动打开定义的路线而不是 Home/Index
ASP.NET page automatically opens with defined route instead of Home/Index
我定义了路由映射以根据控制器中定义的路由值打开页面。
当我在主文件夹下 运行 index.cshtml
时,页面打开 https://localhost:44333/Home/Index
没有结果,但我已经在控制器的索引函数中定义了路由。当我将 url 更改为 https://localhost:44333/MainPage
时,它以 https://localhost:44333/MainPage
.
打开页面
如何在 运行 应用程序 url https://localhost:44333/MainPage
或“https://localhost:44333/
” 时自动打开页面。
这是我 HomeController
的 Index
函数,定义如下。
[RequireHttps]
[Route("")]
[Route("MainPage")]
public ActionResult Index()
{
ViewBag.Attribute = "header-transparent";
return View();
}
这是我的 RouteConfig.cs 文件,如下所示。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
这是我的管理部分
[Route("panel")]
public ActionResult Index()
{
var allCategories = db.Category.ToList();
return View(allCategories);
}
[Route("panel/login")]
public ActionResult Login()
{
return View();
}
当我 运行 应用程序显示为 /Admin/Index
并将 url 显示为 panel/login
时,如何执行相同的过程?
如果您想为您的路线设置不同的 URL,请使用 RoutePrefix
。
这里是一个例子,如果你想打开 panel/login
而不是 /admin/index
:
[RoutePrefix("panel")]
public class AdminController : Controller
对于 ActionResult:
[Route("Login")]
public ActionResult Index()
我定义了路由映射以根据控制器中定义的路由值打开页面。
当我在主文件夹下 运行 index.cshtml
时,页面打开 https://localhost:44333/Home/Index
没有结果,但我已经在控制器的索引函数中定义了路由。当我将 url 更改为 https://localhost:44333/MainPage
时,它以 https://localhost:44333/MainPage
.
如何在 运行 应用程序 url https://localhost:44333/MainPage
或“https://localhost:44333/
” 时自动打开页面。
这是我 HomeController
的 Index
函数,定义如下。
[RequireHttps]
[Route("")]
[Route("MainPage")]
public ActionResult Index()
{
ViewBag.Attribute = "header-transparent";
return View();
}
这是我的 RouteConfig.cs 文件,如下所示。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
这是我的管理部分
[Route("panel")]
public ActionResult Index()
{
var allCategories = db.Category.ToList();
return View(allCategories);
}
[Route("panel/login")]
public ActionResult Login()
{
return View();
}
当我 运行 应用程序显示为 /Admin/Index
并将 url 显示为 panel/login
时,如何执行相同的过程?
如果您想为您的路线设置不同的 URL,请使用 RoutePrefix
。
这里是一个例子,如果你想打开 panel/login
而不是 /admin/index
:
[RoutePrefix("panel")]
public class AdminController : Controller
对于 ActionResult:
[Route("Login")]
public ActionResult Index()