Skip to content

A Razor ViewLocationExpander to serve dedicated views for Weixin/Wechat, tablet, mobile or what-you-need customized user-agent browsers.

License

Notifications You must be signed in to change notification settings

myvas/AspNetCore.ViewDivert

Repository files navigation

ViewDivert

GitHub (Pre-)Release Date ViewDivert github badge NuGet

A Razor ViewLocationExpander to serve dedicated views for Weixin or what-you-need browsers.

Usage

ConfigureServices()

services.AddViewDivert();

Use ViewDivert attribute

  1. Put the attribute on Controller, so effect all actions in this Controller:
/// <summary>
/// YES, ViewDivert on all actions!
/// </summary>
[ViewDivert]
public class HomeController : Controller
{
	public IActionResult Index()
      {
          return View();
      }

	public IActionResult About()
	{
		return View();
	}
}
  1. Put only on the specified action(s):
public class Home2Controller : Controller
{
	/// <summary>
	/// YES, ViewDivert on me!
	/// </summary>
	/// <returns></returns>
	[ViewDivert]
	public IActionResult Index()
      {
          return View();
      }

	/// <summary>
	/// NO, ViewDivert NOT on me!
	/// </summary>
	/// <returns></returns>
	public IActionResult About()
	{
		return View();
	}
}
// Views/Xxx/Yyy.cshtml          (default)
// Views/Xxx/Yyy.weixin.cshtml   (MicroMessenger, via Weixin/Wechat browser)
// Views/Xxx/Yyy.tablet.cshtml   (tablet, eg. iPad, iPad Pro and etc)
// Views/Xxx/Yyy.mobile.cshtml   (mobile, via browser)
// Views/Xxx/Yyy.custom.cshtml   (custom)

IDeviceResolver

public class DeviceController : Controller
{
	private readonly IDeviceResolver _deviceResolver;

	public DeviceController(IDeviceResolver deviceResolver)
	{
		_deviceResolver = deviceResolver;
	}

	public IActionResult Index()
	{
		var deviceCode = _deviceResolver.Resolve(HttpContext);

		ViewData["DeviceCode"] = deviceCode;

		return View();
	}
}

Razor view

@if (DeviceResolver.IsMicroMessenger(Context))
{
    <strong>This browser is/as a Weixin.</strong>
}

Dev

About

A Razor ViewLocationExpander to serve dedicated views for Weixin/Wechat, tablet, mobile or what-you-need customized user-agent browsers.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages