-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBrowserLinkExtensions.cs
More file actions
75 lines (63 loc) · 2.49 KB
/
BrowserLinkExtensions.cs
File metadata and controls
75 lines (63 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.VisualStudio.Web.BrowserLink;
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// Implementation of extension methods for configuring Browser Link
/// in an ASP.NET Core application.
/// </summary>
public static class BrowserLinkExtensions
{
/// <summary>
/// This method is called to enable Browser Link in an application. It
/// registers a factory method that creates BrowserLinkMiddleware for
/// each request.
/// </summary>
public static IApplicationBuilder UseBrowserLink(this IApplicationBuilder app)
{
if (!IsMicrosoftRuntime)
{
return app;
}
try
{
string applicationBasePath;
if (GetApplicationBasePath(app, out applicationBasePath))
{
applicationBasePath = PathUtil.NormalizeDirectoryPath(applicationBasePath);
HostConnectionUtil.SignalHostForStartup(applicationBasePath, blockUntilStarted: false);
BrowserLinkMiddlewareFactory factory = new BrowserLinkMiddlewareFactory(applicationBasePath);
return app.Use(factory.CreateBrowserLinkMiddleware);
}
else
{
// Browser Link doesn't work if we don't have an application path
return app;
}
}
catch
{
// Something went wrong initializing the runtime. Browser Link won't work.
return app;
}
}
private static bool IsMicrosoftRuntime
{
get { return Type.GetType("Mono.Runtime") == null; }
}
private static bool GetApplicationBasePath(IApplicationBuilder app, out string applicationBasePath)
{
IWebHostEnvironment hostingEnvironment = app.ApplicationServices.GetService(typeof(IWebHostEnvironment)) as IWebHostEnvironment;
if (hostingEnvironment != null)
{
applicationBasePath = hostingEnvironment.ContentRootPath;
return applicationBasePath != null;
}
applicationBasePath = null;
return false;
}
}
}