在动作中设置页面html标题

时间:2022-12-04 23:25:08

How can I set the Html title from within a controllers action?

如何在控制器操作中设置Html标题?

2 个解决方案

#1


I don't believe there is a way to simply set the title directly from the controller with out setting up your views to accept some sort of data associated with the information. Especially since to actually set the title you'll need to output data in between tags.

我不相信有一种方法可以直接从控制器设置标题而不设置视图以接受与信息相关的某种数据。特别是因为要实际设置标题,您需要在标签之间输出数据。

That said, I'm sure there's something you could do to make this easier on yourself. I'm more or less just thinking aloud here, so I can't guarantee this will work. If I was sure that I would set my title's on every action that I have, then I would keep the title tags in the master page and create a custom attribute so you could do something like this:

也就是说,我相信你可以采取一些措施让自己更轻松。我或多或少只是在这里大声思考,所以我不能保证这会起作用。如果我确定我会在每个动作上设置我的标题,那么我会在主页面中保留标题标签并创建一个自定义属性,这样你就可以这样做:

[CustomTitleAttribute(Title = "Hello World")]
public ActionResult Index()
{
    return View();
}

It would be up to you to implement the attribute and set up how you capture this information in the view and/or master page.

由您来实现属性并设置在视图和/或母版页中捕获此信息的方式。

Generally speaking, since you may want the title to change on pages that have dynamic data, the above is probably not something you should do. Instead, just incorporate some way to determine the title you need in a view model. Maybe even a base view model that subsequent view models can inherit from.

一般来说,由于您可能希望在具有动态数据的页面上更改标题,因此上述内容可能不是您应该执行的操作。相反,只需合并一些方法来确定视图模型中所需的标题。甚至可能是后续视图模型可以继承的基本视图模型。

public class BaseViewModel
{
    public string PageTitle { get; set; }
    public string PageDescription { get; set; }
    //etc.
}

Then in the views you can do this, or even in your master page I think:

然后在视图中你可以这样做,甚至在你的母版页中我认为:

<title><%= Model.PageTitle %></title>

I think from a standpoint of separating concerns(which is kinda the whole point) that would be the best way to go.

我认为从分离问题的角度来看(这有点重要),这是最好的方法。

#2


It sounds not very wise for controller to control the output. All a controller do, is to give viewdata to the output view and let the view determine what to do with it.

控制器控制输出听起来不是很明智。所有控制器都是将viewdata提供给输出视图,让视图决定如何处理它。

Please refer to the default ASP.net MVC template for how it is done.

请参阅默认的ASP.net MVC模板以了解其完成方式。

#1


I don't believe there is a way to simply set the title directly from the controller with out setting up your views to accept some sort of data associated with the information. Especially since to actually set the title you'll need to output data in between tags.

我不相信有一种方法可以直接从控制器设置标题而不设置视图以接受与信息相关的某种数据。特别是因为要实际设置标题,您需要在标签之间输出数据。

That said, I'm sure there's something you could do to make this easier on yourself. I'm more or less just thinking aloud here, so I can't guarantee this will work. If I was sure that I would set my title's on every action that I have, then I would keep the title tags in the master page and create a custom attribute so you could do something like this:

也就是说,我相信你可以采取一些措施让自己更轻松。我或多或少只是在这里大声思考,所以我不能保证这会起作用。如果我确定我会在每个动作上设置我的标题,那么我会在主页面中保留标题标签并创建一个自定义属性,这样你就可以这样做:

[CustomTitleAttribute(Title = "Hello World")]
public ActionResult Index()
{
    return View();
}

It would be up to you to implement the attribute and set up how you capture this information in the view and/or master page.

由您来实现属性并设置在视图和/或母版页中捕获此信息的方式。

Generally speaking, since you may want the title to change on pages that have dynamic data, the above is probably not something you should do. Instead, just incorporate some way to determine the title you need in a view model. Maybe even a base view model that subsequent view models can inherit from.

一般来说,由于您可能希望在具有动态数据的页面上更改标题,因此上述内容可能不是您应该执行的操作。相反,只需合并一些方法来确定视图模型中所需的标题。甚至可能是后续视图模型可以继承的基本视图模型。

public class BaseViewModel
{
    public string PageTitle { get; set; }
    public string PageDescription { get; set; }
    //etc.
}

Then in the views you can do this, or even in your master page I think:

然后在视图中你可以这样做,甚至在你的母版页中我认为:

<title><%= Model.PageTitle %></title>

I think from a standpoint of separating concerns(which is kinda the whole point) that would be the best way to go.

我认为从分离问题的角度来看(这有点重要),这是最好的方法。

#2


It sounds not very wise for controller to control the output. All a controller do, is to give viewdata to the output view and let the view determine what to do with it.

控制器控制输出听起来不是很明智。所有控制器都是将viewdata提供给输出视图,让视图决定如何处理它。

Please refer to the default ASP.net MVC template for how it is done.

请参阅默认的ASP.net MVC模板以了解其完成方式。