Alternative to ViewBag.Title in ASP.NET MVC 3

前端 未结 8 2010
不思量自难忘°
不思量自难忘° 2021-02-05 05:20

By default the new project template for ASP.NET MVC 3 adds the following to the default layout (masterpage in razor):

@ViewBag.Title
<         


        
8条回答
  •  迷失自我
    2021-02-05 05:36

    I don't think there is any thing bad with default title handling feature that ships with asp.net MVC 3, its okay to do.

    I personally do this(below written) to handle title, I am not endorsing the below code or saying that its better than default functionality, its just a preference.

    Master

    
        @RenderSection("Title");
    
    

    View

    @section Title
    {
        write title
    }
    

    One thing i could suggest to improve default functionality

    @{
        string pageTitle = @ViewBag.Title ?? "Title Not Set";
    }
    @pageTitle
    

    So whenever you forget to add it in viewbag, the page will display title= Title Not Set

    Creating a base class then making all your controllers inherit from that base-class can also be done. But I think its taking so much of pain for title.

提交回复
热议问题