返回另一个控制器中的视图。

huangapple go评论72阅读模式
英文:

How to return a view in another controller

问题

我有一个 .net6 的 MVC 应用程序,我想要初始显示启动页面 Index.cshtml 不是来自 View\Home 文件夹,而是来自另一个目录 AccountUpload\Index.cshtml。另外,控制器被称为 AccountUploadController。

我应该如何在 program.cs 中实现这个呢?

以下是我在 program.cs 中的代码:

app.UseStaticFiles();

app.UseRouting();
app.UseAuthorization();

app.UseMvc();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
英文:

I have a .net6 MVC application and I want the initial display startup page Index.cshtml not from inside the View\Home folder but from another directory AccountUpload\Index.cshtml. Also the controller is called AccountUploadController.

How can I do this from the program.cs?

Here's what I have in program.cs

app.UseStaticFiles();

app.UseRouting();
app.UseAuthorization();

app.UseMvc();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

答案1

得分: 2

好的,你应该可以通过在 MapControllerRoute 方法中键入 "AccountUpload" 而不是 "Home" 来实现你的目标,但我想你已经尝试过这样做了(对吗?),所以这里有一些其他方法可以做到:

  1. 在你的 HomeController 的 Index 操作中:重定向到你的 AccountUploadController 的 Index 操作。
    示例:return RedirectToAction("Index", "AccountUpload");
  2. 在你的 HomeController 的 Index 操作中:通过指定文件路径返回 AccountUpload 文件夹中的 Index.cshtml 视图。
    示例:return View("~/Views/AccountUpload/Index.cshtml");

在这两种情况下,你必须记得向视图传递正确的模型。顺便说一下,正确的方式是在 Program.cs 中指定初始操作和控制器。

英文:

Well, you should be able to achieve your goal just by typing "AccountUpload" instead of "Home" in the MapControllerRoute method but I think you have already tried this (right?) so here are some other ways to do it:

  1. In the Index action in your HomeController: redirect to the Index action in your AccountUploadController.
    Ex: return RedirectToAction("Index", "AccountUpload");
  2. In the Index action in your HomeController: return the view Index.cshtml which is in the AccountUpload folder by specifying the file path.
    Ex: return View("~/Views/AccountUpload/Index.cshtml");

In both cases, you have to remember to pass the proper model to the view. By the way, the proper way is to specificy the inital action and controller in the Program.cs

答案2

得分: 0

> 我有一个 .net6 MVC 应用程序,我希望在初始显示启动页面 Index.cshtml,不是从 View\Home 文件夹内,而是从另一个目录 AccountUpload\Index.cshtml 内。同时,控制器被称为 AccountUploadController。

你可以简单地修改你的 MapControllerRoute 中的模式,这将在应用程序初始化时调用 AccountUpload 页面的索引。

情景:

让我们来看看它的运行方式。假设你的 AccountUpload 控制器有以下目录:

返回另一个控制器中的视图。

程序.cs 中的传统路由:

如果你现在想要默认从 AccountUpload 控制器调用 Index.cshtml 页面,你可以修改你的 传统路由定义 如下:

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=AccountUpload}/{action=Index}/{id?}");

输出:

返回另一个控制器中的视图。

注意: 如果你想了解更多关于 传统路由定义 的详细信息,你可以在这里查看我们的官方文档

英文:

> I have a .net6 MVC application and I want the initial display startup
> page Index.cshtml not from inside the View\Home folder but from
> another directory AccountUpload\Index.cshtml. Also the controller is
> called AccountUploadController.

You could simply modify your pattern within MapControllerRoute which would call your AccountUpload page index while the application being initialized.

Scenario:

Let's have a look in action. Assuming your have following directory for your AccountUpload controller:

返回另一个控制器中的视图。

Conventional route in program.cs:

If you now want to call your Index.cshtml page from AccountUpload controller by default you could modify your conventional route defination as following:

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=AccountUpload}/{action=Index}/{id?}");

Output:

返回另一个控制器中的视图。

Note: If you would like to know more details on conventional route defination you could check our official document here

huangapple
  • 本文由 发表于 2023年3月20日 23:42:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定