以下是一个使用ASP.NET MVC的路由和子视图的示例代码:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
}
在Views文件夹中创建Home文件夹,并在其中创建两个子视图:Index.cshtml和About.cshtml。
在Index.cshtml中添加以下内容:
Welcome to my website!
This is the home page.
About
This is the about page.
你将看到对应的子视图被加载和显示在浏览器中。
希望这能帮助到你!