我查了很多其他问题,但找不到解决办法。
在一家需要我使用asp.net的新公司工作,我来自一个更大的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>
我有一个aboutcontroller文件在views/about中有一个索引文件。
home/route工作正常,但是/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();
}
}
}