MVC3 Meta tag Dynamically change - asp.net-mvc-3

I am working on MVC3, I am using Razor engine i have a layout.cshtml page which contains meta tag like
<meta property="og:url" content="http://www.mywebsite.com/" />
I have some content pages like abc.cshtml which has Layout.cshtml page as there's layout,so they are getting meta tags from layout.cshtml,i want that abc.cshtml should have its own meta tag like
<meta property="og:url" content="http://www.mywebsite.com/controller/abc" />
How can i do this? OR can i change meta tag dynamically??????

Yes you can pass that value from each or view action in ViewBag like below...
ViewBag.OgURL = "http://www.mywebsite.com/controller/abc";
then in you layout.cshtml
#ViewBag.OgURL
or set above head tag in you layout.cshtml..
#RenderSection("head", false)
and add in your view...
#section head {
<meta property="og:url" content="http://www.mywebsite.com/controller/abc" />
}

Related

Domain redirection and url masking

The image uploaded shows the post preview thumbnail of facebook.
(1)Consider a link x.com/abc which is redirected to y.com/xyz.
(2)When the link x.com/abc is shared on facebook it's preview is fetched from y.com/xyz.
(3)The preview thumbnail generated by facebook contains the domain y.com and not the x.com.
(4)How do we make the preview thumbnail show x.com and not the target url y.com.
The facebook screenshot show's that I shared url hivirality.com/abhishek which is redirected on citryxsolutions.in but the preview thumbnail contains the domain name of targeted url i.e citryxsolutions.in.
What are the possible cases to fix the issue.
You can do that, but it is a bit tricky. Instead of 301 redirect from x.com/abc to y.com/xyz. Your server should return HTML, which will redirect user through JS. This HTML should have Open Graph markup which Facebook is parsing. For example:
<!doctype html>
<html lang='en'>
<head>
<meta charset="utf-8" />
<meta property="og:url" content="URL" />
<meta property="og:title" content="TITLE" />
<meta property="og:type" content="website" />
<meta property="og:description" content="DESCRIPTION" />
<meta property="og:image" content="IMAGE URL" />
</head>
<body>
<script type="text/javascript">
if("false" == "false") {
window.location = "DESTINATION URL";
}
</script>
</body>
</html>
Where URL is x.com/abc
And DESTINATION URL is y.com/xyz
And you can check how Facebook reacts on that in FB debugger (also you can clear cache here): https://developers.facebook.com/tools/debug/

Regarding page title & meta tag generation from server end and render in page

I searching for a tips to generate page title & meta tag from server end and render in page.
I got this trick.
First approach would be to Make a parent interface for all your model objects. and you could have:
public interface IBaseMasterViewDto
{
int PageId { get; set; }
string Title { get; set; }
string MetaKeywords { get; set; }
string MetaDescription { get; set; }
}
Thus in your master view you could use
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<IBaseMasterViewDto>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title><%: Model.Title %></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="keywords" content="<%: Model.MetaKeywords %>" />
<meta name="description" content="<%: Model.MetaDescription %>" />
If there is no concept like master page in MVC3 so guide me how can i implement the above code for layout page or make the above code compatible for mvc3 layout page. thanks
If there is no concept like master page in MVC3 so guide me how can i implement the above code for layout page or make the above code compatible for mvc3 layout page.
Let's first try to clear the notions here. ASP.NET MVC 3 is a server side framework allowing to develop web applications on the top of ASP.NET implementing the MVC pattern.
It supports different views for rendering the markup:
ASP.NET WebForms
Razor
...
The ASP.NET WebForms view has notion of MasterPage files (.master). The Razor view has the same equivalent called Layouts (.cshtml).
So you could apply exactly the same notion in a Razor Layout file:
#model IBaseMasterViewDto
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>#Model.Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="keywords" content="#Model.MetaKeywords" />
<meta name="description" content="#Model.MetaDescription" />
...

want to put #renderbody() into a partial view source by _layout

Creating an MVC 3 Razor project. have a very involved UI design. I wanted to put #renderbody() into a partial view source by _layout. The compiler won't let me. Is there a way to do this?
Instead of partial view you can go for master/sub layouts.
MasterLayout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>#ViewBag.Title</title>
</head>
<body>
#RenderBody()
</body>
</html>
Layout.cshtml
#{
Layout = "~/Views/Shared/_MasterLayout.cshtml";
}
// html code above render body
#RenderBody()
// html code below render body
Your sublayout(Layout.cshtml) contains the code that should be in the partial view.

How to exclude some content from _LyoutView being added to specific view

I have such script on my _LyoutView:
<noscript>
<meta http-equiv="refresh" content="0; URL=/Noscript">
</noscript>
But when user with javascript disabled redirected to that page, the infinity request of that page taking place because that page contains this code above as well.
So i just thought may be there is some things out of the box in ASP.NET MVC that can help?
Basically i want that that code from _LyoutView to be added to all views except Noscript view.
i can go like that on _LyoutView:
#if (ViewContext.Controller.ValueProvider.GetValue("action").RawValue != "Noscript")
{
<noscript>
<meta http-equiv="refresh" content="0; URL=/Noscript">
</noscript>
}
But may be there is more better way of doing that?
You can check the RawUrl property of the Request object to see if it contains the term "Noscript" which I'm assuming won't be part of the URL unless we're on that page.
#if(!Request.RawUrl.Contains("Noscript"))
{
<noscript>
<meta http-equiv="refresh" content="0; URL=/Noscript">
</noscript>
}

Razor Engine - SEO Meta tags

I need to place unique description and keywords meta tags to each page. Tried this. But its not clicking.
#{
ViewBag.Title = "Title";
ViewBag.Description = "Test";
ViewBag.Keywords = "Test1, Test2, Test3";
}
How do I place the meta tags in MVC 3 Razor Engine?
In the layout you could define a section:
<head>
...
#RenderSection("metas")
</head>
and in view:
#section metas
{
<meta name="description" content="Test" />
<meta name="title" content="Title" />
<meta name="keywords" content="Test1, Test2, Test3" />
...
}
or in the layout:
<head>
<meta name="description" content="#ViewBag.Description" />
<meta name="title" content="#ViewBag.Title" />
<meta name="keywords" content="#ViewBag.Keywords" />
...
</head>
and in the view:
#{
ViewBag.Title = "Title";
ViewBag.Description = "Test";
ViewBag.Keywords = "Test1, Test2, Test3";
}
You can do as you did, but you have to link them in your _Layout.cshtml. Add this to your _Layout.cshtml in the <head> section:
#if(ViewBag.Description!=null)
{
<meta name="description" content="#ViewBag.Description" />
}
#if(ViewBag.Keywords!=null)
{
<meta name="keywords" content="#ViewBag.Keywords" />
}
You need to emit <meta> tags in your layout page that use the values.
You can get the values in the tags by writing #ViewBag.Description.

Resources