Coder Social home page Coder Social logo

sebnilsson / aspnetseo Goto Github PK

View Code? Open in Web Editor NEW
55.0 13.0 7.0 684 KB

Helpers for handling the SEO-data for ASP.NET web-applications

Home Page: https://www.nuget.org/packages/AspNetSeo/

C# 95.64% HTML 4.36%
aspnet aspnetcore aspnet-mvc asp-net asp-net-core asp-net-core-mvc asp-net-mvc seo csharp

aspnetseo's Introduction

ASP.NET SEO

Helpers for handling the SEO-data for ASP.NET web-applications.

Provides a SeoHelper-class which is easily accessible in Controllers, Views and through ActionFilterAttributes.

SeoHelper

Properties

The SeoHelper-class exposes multiple properties to get or set multiple SEO-related data:

  • LinkCanonical: Gets or sets the canonical link for web-page.

    • SiteUrl: Gets or sets the main URL for web-site. Used as base for the canonical link.
  • MetaDescription: Gets or sets the meta-description for web-page.

  • MetaKeywords: Gets or sets the meta-keywords for web-page.

  • MetaRobots: Gets or sets meta-robots instructions web-page.

  • OgDescription: Gets or sets the open graph description for web-page. Falls back on value in MetaDescription.

  • OgImage: Gets or sets the open graph image for web-page.

  • OgSiteName: Gets or sets the open graph site-name for web-page. Falls back on value in SiteName.

  • OgTitle: Gets or sets the open graph title for web-page. Falls back on value in PageTitle.

  • OgType: Gets or sets the open graph type for web-page.

  • OgUrl: Gets or sets the open graph URL for web-page. Falls back on value in LinkCanonical.

  • PageTitle: Gets or sets the title for a web-page.

    • SiteName: Gets or sets the name for the web-site. Used as base for DocumentTitle.
  • DocumentTitle: Gets the document-title for a web-page. Combines PageTitle and SiteName.

    • DocumentTitleFormat: Gets or sets the format for the document-title. Default value is {0} - {1}, where {0} is the value from PageTitle and {1} is the value from SiteName.

Methods

  • SetCustomMeta(string key, string value): Add any custom meta-tag.
  • SetMetaRobots(bool index, bool follow): Specify the instructions for robots. Updates the value for MetaRobots.

Notes

LinkCanonical can be set as absolute URL (https://example.com/section/page.html), as a relative URL (/section/page.html) or using ASP.NET's app-relative URL-format (~/section.page.html). Relative URLs will automatically get converted to absolute URLs, either from provided SiteUrl or from the base of the requested URL.

ActionFilterAttributes

The properties exposed by the SeoHelper-class all have corresponding action-filter-attributes are available for Controllers and Controller-actions.

For example, [PageTitle] can be used for an action, if no logic is needed for the value, while [SiteName] can be used for a controller. Using another [SiteName]-attribute on an action will override the one used on the controller.

Examples of attribute-usage:

[SiteName("Website name")]
[SiteUrl("https://production-url.co/")]
public class InfoController : SeoController
{
    [PageTitle("Listing items")]
    [MetaDescription("List of the company's product-items")]
    public ActionResult List()
    {
        var list = GetList();
        
        if (list.Any())
        {
            Seo.PageTitle += $" (Total: {list.Count})";
            Seo.LinkCanonical = "~/pages/list.html";
        }
        else
        {
            Seo.SetMetaRobots(index: false, follow: true);
        }

        return View(model);
    }
}

ASP.NET Core MVC

Configuration

To register the SEO-helper as a service for Dependency Injection you just need to use the framework's provided extension-method in the ConfigureServices method inside Startup.cs:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddSeoHelper();
    }
}

Accessing the SeoHelper-class

With the configured Dependency Injection you can access the SeoHelper-class through constructor-injection or by accessing the RequestServices-object in the HttpContext.

The framework provides an extension-method for IServiceProvider for getting the SeoHelper-instance:

public IActionResult Edit()
{
    var seoHelper = HttpContext.RequestServices.GetSeoHelper();
    // ...
}

Tag Helpers

Tag Helpers are available to render the values set through the SeoHelper-class. They also expose attributes to override or set the values on the fly in the markup.

Examples: <document-title /> renders the combined PageTitle and SiteName. <link-canonical /> renders the canonical URL for the page. <og-url /> renders the set URL for the page and falls back to value used in <link-canonical />.

Individual tags will not be rendered if there is no valid data provided for them, either through the SeoHelper-class or exposed attributes.

Setting default values

To set the webb-application’s default base-title and default base canonical link, which can be overridden if needed, the configuration during registering the service for Dependency Injection can be used:

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddSeoHelper(
        siteName: "Website name",
        siteUrl: "https://production-url.co/");
}

ASP.NET MVC

Support for classic ASP.NET MVC is deprecated.

aspnetseo's People

Contributors

sebnilsson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aspnetseo's Issues

"Custom" og tags

Hello,
I am trying to add og tags with CustomMetas, but ones that are not build-in like "locale", like in #2 they are output with "name" attribute instead of "property" . Maybe there should be CustomOgTags or something like that ?
Also og tag can be array, so multiple tags with same key can exist.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.