如何将静态页面作为MVC应用程序的默认操作

本文关键字:应用程序 MVC 默认 操作 静态 | 更新日期: 2023-09-27 18:26:09

我在根目录中有MVC应用程序和一个静态页面(恰好被称为index.html)。

我正试图找到一种方法,在网站首次加载时为这个静态页面提供服务,即默认操作应该是向我网站的访问者提供静态页面。

如何在我的申请中做到这一点?

如何将静态页面作为MVC应用程序的默认操作

检查App_Start文件夹中的RouteConfig.cs文件并更改以下路由

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

它应该是index.cshtml而不是index.html。相应地更改控制器属性。

您还需要将以下代码添加到您的控制器

public actionresult Index()
{ 
    return View();
}

享受编码:)

假设您有一个家庭控制器,并且您了解

家庭控制器

Public ActionResult Index() 
{
    return View(); //or you can choose to do this: return View("~/Index.cshtml") <- specifying the document path explicitly 
}

它设置在App_Start/RouteConfig.cs文件中

routes.MapRoute(
    "Default",
        "{controller}/{action}/{id}",
            new { controller = "Login", action = "Index", id = UrlParameter.Optional },
            new[] { "CIS.PresentationLayer.Controllers" }
        );

当MVC应用程序运行时,此路由将启动LoginCOntroller的"索引"视图。

还要注意,您将需要一个控制器,例如

 public class LoginController : Controller
 {
    [HttpGet]
    public ActionResult Index()
    {
        return View(new LoginViewModel() { Authenticated = true } );
    }
 }