简体   繁体   English

.Net MVC控制器使用相同的cshtml作为视图使用不同的路由名称

[英].Net MVC Controller different route name using the same cshtml as view

In .NET MVC, I see it always follows this naming convention: RouteName in the controller and it is a view called 'RouteName.cshtml'. 在.NET MVC中,我看到它始终遵循以下命名约定:控制器中的RouteName,它是一个名为“ RouteName.cshtml”的视图。

My question is how can I use SAME cshtml file with different RouteName? 我的问题是如何使用带有不同RouteName的SAME cshtml文件?

The default View() method defaults to matching the Action name, but you can use the View(string) method to specify that path to your desired CSHTML, like in the following example: 默认的View()方法默认与Action名称匹配,但是您可以使用View(string)方法来指定所需CSHTML的路径,如以下示例所示:

// in action method that is *not* RouteName
return View("RouteName");

You can re-use chunks of CSHTML with partials ; 您可以使用带有partials的CSHTML块; depending on the problem that you're trying to solve, this may be a better solution. 根据您要解决的问题,这可能是一个更好的解决方案。

Can you edit your question and add more context? 您可以编辑问题并添加更多上下文吗? Why do you need to render the same cshtml from more than one ActionResult method? 为什么需要通过多个ActionResult方法呈现相同的cshtml? What are you trying to accomplish? 你想达到什么目的?

For example, you have a view named About.cshtml You have 2 action methods and both of them are using About.cshtml 例如,您有一个名为About.cshtml的视图,您有2个操作方法,并且两个方法都使用About.cshtml

So you can try 所以你可以尝试

public ActionResult Index()
        {
        var model = "view model";
        // Pass view model as the second parameter
        return View("About", model);
        }

public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";
            // Not need pass view name because view name and action method name are same (About
            return View();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 .net 核心 5 MVC 中重载操作方法但路由模板不同(名称为 controller,名称为 controller) - Overload action method but different route template(with controller name and without controller name) in .net core 5 MVC 创建具有相同名称但路径前缀不同的其他控制器 - Creating different controller with same name but different route Prefix 具有相同控制器名称(不同名称空间)的ASP.net MVC单独解决方案 - ASP.net MVC seperate solutions with same controller name (different namespace) ASP.NET MVC5,两个控制器,不同文件夹同名 - ASP.NET MVC5, two controller, with the same name in different folders 在ASP.net 5 MVC 6中,如何在不同的命名空间中使用相同的控制器名称 - In ASP.net 5 MVC 6 , How to use same controller name in different namespaces ASP.NET MVC-从不同的视图/控制器调用相同的存储过程 - ASP.NET MVC - call the same stored procedure from a different view/controller ASP.NET MVC 5 - 获取当前视图的名称(Razor .cshtml 端) - ASP.NET MVC 5 - Get current view's name (Razor .cshtml side) asp.net mvc路由使用表单get具有2个动作和相同的视图 - asp.net mvc route using form get with 2 action and same view 如何在ASP.NET MVC中将/ controller / action-name之类的URL路由到controller / action_name - How to route URLs like /controller/action-name to controller/action_name in ASP.NET MVC .net核心2 mvc中相同操作的不同路由和身份验证 - different route and auth for the same action in the .net core 2 mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM