c# - 带 View 的 BaseController

标签 c# asp.net-mvc

我应该创建一个包含多个部分的网站。这些部分的功能和 View 完全相同,但我想要不同的 URL,例如

  • //localhost:111/Works/Index
  • //localhost:111/OldJobs/Index

出于这个原因,我创建了一个包含我需要的所有 ActionResultBaseReferenceController 。例如:

public class BaseReferenceController : Controller
{
    public virtual ActionResult Index(int? sectionId)
    {
        Articles articles = GetArticles(sectionId);
        // more code base on sectionId
        return View(articles);
    }

    // more ActionResult
}

现在我创建一个新的 Controller 来拥有一个新的 URL Works ,例如

public class WorksController : BaseReferenceController
{
    public override ActionResult Index(int? sectionId)
    {
        return base.Index(2);
    }
}

发生错误

The view 'Index' or its master was not found or no view engine
supports the searched locations. The following locations were
searched: 
~/Views/Works/Index.aspx 
~/Views/Works/Index.ascx
~/Views/Works/Index.aspx 
~/Views/Shared/Index.ascx
~/Views/Works/Index.cshtml 
~/Views/Works/Index.vbhtml
~/Views/Shared/Index.cshtml 
~/Views/Shared/Index.vbhtml

我的意图是 BaseReferenceController 创建页面,而 WorksController 返回来自 BaseReferenceControllerActionResult

我尝试使用RedirectToActionPermanent,但无法获得我想要的结果。

更新 忘记面包屑。

更新/2

根据@ironstone13的回答(谢谢!)我尝试创建一条新路线:

routes.MapRoute(
    "Works",
    "Works/{action}/{id}/{slug}",
    new { controller = "BaseReferenceController", action = "Index", 
          sectionId = 2, slug = UrlParameter.Optional },
    new { id = @"\d+" });

其中 action 是我的 Controller 中的 actionid 是要显示或的记录的标识符编辑(如果有必要),slug是slug:)但我需要一个空间来传递sectionId(因为没有这个我不知道如何过滤结果) .

我在 Insight debug 中收到一条消息

The controller for path '/Works' was not found or does not implement IController.

在 Visual Studio 中我收到了这个

System.InvalidOperationException occurred HResult=0x80131509
Message=The constraint entry 'slug' on the route with route template 'Works/{action}/{sectionId}/{slug}' must have a string value or be of a type which implements 'System.Web.Routing.IRouteConstraint'.
Source=System.Web.Mvc

我哪里错了?之后我可以打电话

  • //localohost:111/Works/Index
  • //localohost:111/Works/New/2
  • //localohost:111/Works/View/23/Document-for-client

?谢谢

最佳答案

一种选择是使用 custom Action Filter并将面包屑逻辑放置在那里,类似于此处所做的操作:How would you implement a breadcrumb helper in asp.net mvc?

可以说,面包屑不是 Controller 方法的核心功能,因此使用 AOP 是有意义的。以通过使用属性应用的自定义操作文件管理器的形式,并且此类基础设施不应成为定义类层次结构的主要因素。

您当前的代码不起作用,因为索引 View 是使用调用 Controller 名称按惯例搜索的,而且显然,您没有基本 Controller 的 View ,而且您很可能不会- 我假设您需要为每个实体(Works 和 OldJobs)提供单独的 View - 那么您将如何指向正确的 View ?

最好避免完全传递 View 路径,并且不要从基类调用 View()。

如果 Works 和 OldJobs 的 View 和 Controller 完全相同 - 只有一个 View 和一个 Controller 是有意义的,并使用路由将不同的 URL 映射到一个 Controller 操作,如下所示

public class MvcApplication : HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "Works",
            "Index",
            new { controller = "BaseReferenceController", action = "Index" });

        routes.MapRoute(
            "OldJobs",
            "Index",
            new { controller = "BaseReferenceController", action = "Index" });

更新:如果您需要在 URI 中传递多个参数,请考虑为您的参数定义单独的类型并使用来自 uri 的参数绑定(bind),请参阅 parameter binding from uri 。 这里描述了类似的方法Routing with Multiple Parameters using ASP.NET MVC

您的 uri 可能如下所示://localohost:111/Works/View/?id=23&slug=Document-for-client§ionId=2。 对于使用 FromUri 参数绑定(bind)进行 GET 方法的多个参数,如果您的参数中没有某种逻辑层次结构,那么将所有参数放在 URI 的 QueryString 部分中会更加清晰。

如果您不将参数放入查询字符串中,那么您的 URI 就会变得不明确。 //localohost:111/Works/View/1/2 是什么意思?是 Id=1 且SectionId=2 还是反之亦然?

关于c# - 带 View 的 BaseController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820633/

相关文章:

c# - 同一解决方案中的 MVC 和 Web Api 项目

asp.net-mvc - 在 IdentityServer 中获取 FacebookAuthenticationProvider 实例(Owin 应用程序)

c# - 将类型作为属性参数传递

c# - Asp.net Core 2.1 日志记录

c# - ASP.NET MVC ActionFilterAttribute 在模型绑定(bind)之前注入(inject)值

asp.net-mvc - asp.net mvc - 限制对网页的访问

c# - 室内Wi-Fi定位API?

c# - 如何将 enum 中的所有元素列出到相同类型的 IEnumerable 数组中?

c# - 等待清除浏览器缓存

asp.net-mvc - Mvc azure存储,一定时间后自动删除存储