基地查看页面在ASP.NET MVC 6 [英] Base View Page in ASP.NET MVC 6

查看:116
本文介绍了基地查看页面在ASP.NET MVC 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC 5我用了一个基础的ViewPage有几个属性:

On ASP.NET MVC 5 I used a base ViewPage with a few properties:

public String PageTitle { get; set; }
public String PageDescription { get; set; }
public String[] BodyCssClasses { get; set; }

然后在每个视图我将有:

Then on each view I would have:

@{
  PageTitle = "Title ..."
  PageDescription" = "Description ..."
  BodyCssClasses = new String[] { "main", "home" }
}

在母版页我想简单地使用类似:

On the master page I would simply use something like:

<title>@Title</title>

通过这种方式,我能够使用强类型的页面属性...

With this approach I was able to use Strong Typed for page properties ...

是否有可能在ASP.NET MVC 6使用基本视图页?

Is it possible to use a Base View Page in ASP.NET MVC 6?

由于没有Web.Config中怎么能这样做?

Since there is no Web.Config how could this be done?

有更好的选择,任何建议,以定义页面头部的信息是值得欢迎的。

Any suggestions for better options to define page head info is welcome.

更新

我跟着建议和我使用:

public abstract class ViewPageBase<TModel> : RazorPage<TModel> {
  public String Title { get; set; }
} // ViewPageBase

然后在_ViewImports我有:

Then on _ViewImports I have:

@inherits ViewPageBase<TModel>

在_Layout.cshtml我有:

On _Layout.cshtml I have:

<title>@Title</title>

终于在它使用一种观点认为,布局我有:

And finally on a view which uses that layout I have:

@{
  Title = "Page Title";
  Layout = "_Layout";
}

一切编译和运行而网页的标题总是空的。

Everything compiles and runs but the page title is always empty.

有没有人有任何想法,为什么?

Does anyone has any idea why?

推荐答案

您可能会想使你的基础视图页面继承 RazorPage

You'll probably want to make your base view page inherit from RazorPage.

public abstract class ViewPageBase<TModel> : RazorPage<TModel>
{
}

那么你应该能够配置所有的网页都从这个在 _ViewImports.cshtml 文件继承。

@inherits ViewPageBase<TModel>

更新

不知道这是最好的方法,但我不知道是否可以使用常见的ViewBag您查看和布局之间共享数据。

Not sure if this is the best approach, but I wonder if you could use the common ViewBag to share data between your View and the Layout.

与ViewBag后面的特性在你的基本页面类:

Back the properties in your base page class with the ViewBag:

public abstract class ViewPageBase<TModel> : RazorPage<TModel>
{
    public string Title
    {
        get { return ViewBag.Title; }
        set { ViewBag.Title = value; }
    }
}

设置属性在您的视图:

Set the property in your view:

@{
    Title = "Home Page"; 
}

使用属性在_Layout.cshtml:

Use the property in _Layout.cshtml:

<title>@Title</title>

这篇关于基地查看页面在ASP.NET MVC 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆