gpt4 book ai didi

c# - .NET MVC 显式 View

转载 作者:行者123 更新时间:2023-11-30 14:41:49 25 4
gpt4 key购买 nike

嘿,这里又来了一位新手,正在玩弄 .NET MVC。我的主要任务是在 URL 上创建一些半静态页面,例如:

  • /关于/
  • /关于/联系人/
  • /关于/工作/

我正在使用一个名为 Static 的 Controller ,并附加了以下路由:

routes.MapRoute(
"About",
"about/{id}",
new { controller = "Static", action = "Index", id = UrlParameter.Optional }
);

它似乎工作正常,因为我有带有 Index 方法的静态 Controller ,该方法使用 switch 语句来识别必须查看哪个页面。我使用 RedirectToAction() 函数调用静态 Controller 的其他操作,以便显示具有其他 View 的页面。我的观点是:

  • /静态/About.aspx
  • /静态/Contacts.aspx
  • /静态/Jobs.aspx

这个方法似乎工作正常,但我不喜欢它的是重定向,所以浏览到/about/contacts 我得到一个重定向到/Static/Contacts 这不是我真正想看到的在 URL 中。

所以我的问题是 - 这样做的正确方法是什么?有没有办法从我的 Index 操作中显式调用某个 View ?

谢谢,~ K.

最佳答案

不要进行重定向。不要在 Index 操作中使用 switch 语句,而是为每个页面(即关于、联系人、工作)设置一个单独的操作,每个页面都有自己的 View 。

你的 Static Controller 看起来像这样:

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

public ActionResult About()
{
return View();
}

public ActionResult Contacts()
{
return View();
}

public ActionResult Jobs()
{
return View();
}

如果您需要对 ContactsJobs 进行任何处理,可以在它们各自的操作中完成。

显式调用某个 View :

return View("ViewName");

View() 方法有七个重载。其中一些允许您传递模型:

return View("ViewName", Model);

关于c# - .NET MVC 显式 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3752060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com