ASP.NET MVC에서 페이지의 메타 태그를 전달하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/144088

  •  02-07-2019
  •  | 
  •  

문제

저는 지난 며칠 동안 ASP.NET MVC를 사용하고 있으며 작은 사이트를 만들 수있었습니다. 모든 것이 잘 작동합니다.

이제 ViewData를 통해 페이지의 메타 태그 (제목, 설명, 키워드 등)를 전달해야합니다. (마스터 페이지를 사용하고 있습니다).

어떻게 처리하고 있습니까? 미리 감사드립니다.

도움이 되었습니까?

해결책

내가 현재하고있는 방법은 다음과 같습니다 ...

MasterPage에는 기본 제목, 설명 및 키워드가있는 콘텐츠 장소 홀더가 있습니다.

<head>
<asp:ContentPlaceHolder ID="cphHead" runat="server">
    <title>Default Title</title>
    <meta name="description" content="Default Description" />
    <meta name="keywords" content="Default Keywords" />
</asp:ContentPlaceHolder>
</head>

그런 다음 페이지 에서이 모든 콘텐츠를 무시할 수 있습니다.

<asp:Content ID="headContent" ContentPlaceHolderID="cphHead" runat="server">
    <title>Page Specific Title</title>
    <meta name="description" content="Page Specific Description" />
    <meta name="keywords" content="Page Specific Keywords" />
</asp:Content>

이것은 당신에게 그것을 설정하는 방법에 대한 아이디어를 제공해야합니다. 이제이 정보를 ViewData (ViewData [ "pagetitle"])에 넣거나 모델 (ViewData.Model.Metadescription- 블로그 게시물 등에 적합)에 포함시킬 수 있습니다.

다른 팁

ViewData에 넣으십시오! 다음과 같은 일을하십시오 ...

BaseViewData.cs- 이것은 다른 모든 ViewData 클래스에서 상속 될 ViewData 클래스입니다.

public class BaseViewData
{
    public string Title { get; set; }
    public string MetaKeywords { get; set; }
    public string MetaDescription { get; set; }
}

그런 다음 사이트 마스터 (또는 무엇이든) 클래스는 다음과 같이 정의되어야합니다.

public partial class Site : System.Web.Mvc.ViewMasterPage<BaseViewData>
{
}

이제 귀하의 사이트에서 마스터 페이지는 간단합니다

<title><%=ViewData.Model.Title %></title>
<meta name="keywords" content="<%=ViewData.Model.MetaKeywords %>" />
<meta name="description" content="<%=ViewData.Model.MetaDescription %>" />

그리고 당신은 웃고 있습니다!

hths, 찰스

추신. 그런 다음이 아이디어를 확장 할 수 있습니다. 예를 들어, 예를 들어 getter를 사용자 (iprincipal) 클래스에 loggedinbaseviewdata 클래스에 넣습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top