Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Using @addTagHelper in ASP.NET Core WebAPI #1002

Closed
NiceStepUp opened this issue Jun 1, 2017 · 4 comments
Closed

Using @addTagHelper in ASP.NET Core WebAPI #1002

NiceStepUp opened this issue Jun 1, 2017 · 4 comments

Comments

@NiceStepUp
Copy link

NiceStepUp commented Jun 1, 2017

I am following this tutorial of "Microsoft.AspNetCore.SpaServices Server Side Prerendering" and there are two important steps to use power "Microsoft.AspNetCore.SpaServices":

1. Go to your "Views/_ViewImports.cshtml" file, and add the following line:
@addTagHelper "*, Microsoft.AspNetCore.SpaServices"`

2. Choose a place in one of your MVC views where you `want to prerender a SPA component. For example, open "Views/Home/Index.cshtml", and add markup like the following:

<div id="my-spa" asp-prerender-module="ClientApp/boot-server"></div>

But "Web Api" does not have "Views" folder. How can I do above steps in Web API?

@SteveSandersonMS
Copy link
Member

In WebApi you're not using Razor views, so it doesn't make sense to use tag helpers. Instead, look into using the Microsoft.AspNetCore.SpaServices.Prerendering.Prerenderer.RenderToString method directly from your C# code. Example usage here: #607

@NiceStepUp
Copy link
Author

NiceStepUp commented Jun 5, 2017

I've developed an web application built using ASP.NET Core Web API and Angular 4. My module bundler is Web Pack 2.
So I cannot use this piece of code: ViewData["SpaHtml"] = prerenderResult.Html; from my controller. I do not have any cshtml views., so I cannot send data from controller to view through ViewData["SpaHtml"].

But I would like to use server side prerendering. I would like to add metatags into head:

import { Meta } from '@angular/platform-browser';

constructor(
    private metaService: Meta) {
}

let newText = "Foo data. This is test data!:)";
    //metatags to publish this page at social nets
    this.metaService.addTags([
        // Open Graph data
        { property: 'og:title', content: newText },
        { property: 'og:description', content: newText },        { 
        { property: "og:url", content: window.location.href },        
        { property: 'og:image', content: "http://usiter.com/uploads/20111118/zhivotnie+koshki+kartinka+s+malenkim+kotyonkom+35121656913.jpg" }]);

Is it possible to do without "ViewData"?`

@MarkPieszak
Copy link
Contributor

You're going to have to create all the tags and add them to your rendered HTML.

@NiceStepUp
Copy link
Author

@MarkPieszak yeah, I've added these tags into head by metaService:

import { Meta } from '@angular/platform-browser';

constructor(
private metaService: Meta) {
}

let newText = "Foo data. This is test data!:)";
//metatags to publish this page at social nets
this.metaService.addTags([
// Open Graph data
{ property: 'og:title', content: newText },
{ property: 'og:description', content: newText }, { 
{ property: "og:url", content: window.location.href }, 
{ property: 'og:image', content: "http://www.freeimageslive.co.uk/files
/images004/Italy_Venice_Canal_Grande.jpg" }]);

and rendered html at browser looks like this when I inspect this element in a browser:

<head>    
    <meta property="og:title" content="<span style=&quot;color:red&quot;>Foo data. This is test data!:)</span>">    
    <meta property="og:description" content="<span style=&quot;color:red&quot;>Foo data. This is test data!:)</span>">
    <meta name="og:url" content="http://foourl.com">
	<meta property="og:image" content="http://www.freeimageslive.co.uk/files
/images004/Italy_Venice_Canal_Grande.jpg"">    
</head>

However, view source in a browser does not have any rendered html.

I've developed an web application built using ASP.NET Core Web API and Angular 4. My module bundler is Web Pack 2.
So I cannot use this piece of code: ViewData["SpaHtml"] = prerenderResult.Html; from my controller. I do not have any cshtml views., so I cannot send data from controller to view through ViewData["SpaHtml"].

But I would like to use server side prerendering to be crawled by SEO I can post my news from my web site by Google, Facebook, Twitter, other social nets.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants