我看了很多其他问题,但找不到解决方法。
在一家要求我使用ASP.NET的新公司中从事某项工作,我来自Laravel PHP世界,那里的路由有些相似,因此ASP.NET路由不会让人感到陌生。
我正在尝试为新站点设置我的基本页面,并使路线最先走,这就是我的经验
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "About",
url: "about-us",
defaults: new { controller = "About", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { }
);
}
在Laravel中,除了不需要默认路由外,我的操作与我类似。
在标题视图中,我有一个基本的导航:
<ul>
<li><a href="@Url.RouteUrl("Root")">Home</a></li>
<li><a href="@Url.RouteUrl("About")">About Us</a></li>
</ul>
我在Views / About中有一个AboutController文件和一个索引文件。
home /路线工作正常,但/ about-us URL返回:
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /about-us
这是我的错误?如果转到URL,但我尚未注册路由,则会收到另一个404错误,因此至少我正在选择路由。
如果我将About路由的控制器更改为Home控制器,则不会返回错误。
这是AboutController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace leadstream.co.uk.Controllers
{
public class AboutController : Controller
{
public ActionResult Index()
{
return View();
}
}
}