How to return an embedded static html file from an Asp.Net Core Controller?
I have the following configuration:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseDefaultFiles();
app.UseFileServer(new FileServerOptions
FileProvider = new EmbeddedFileProvider(typeof(Startup).Assembly, typeof(Startup).Namespace + ".WebApp"),
RequestPath = string.Empty
);
app.UseCors("CorsPolicy");
app.UseMvc(routes =>
routes.MapRoute(
name: "angular",
defaults: new controller = "Angular", action = "Index",
template: "*url");
);
My Angular project files are in the namespace MyNamespace.WebApp
.
My AngularController
and Startup
classes are in the namespace MyNamespace
When I don't use MVC and access http://localhost:8000 it loads the index.html
file in the WebApp
folder. Now for all other requests (for example /action
) I have mapped it to the AngularController
, which looks as follows:
public class AngularController : Controller
public IActionResult Index()
return View("/index.html");
I have debugged and verified that the request does come to AngularController.Index()
and returns View("/index.html")
. But after that I get a 500 Error. I'm guessing because it cannot find the view file.
How do I let MVC know to fetch the index.html
file from the embedded files?
I've tried the following:
return View("~/index.html");
return View("index.html");
return View("~/../index.html");
return View("../index.html");
return View("~/WebApp/index.html");
return View("WebApp/index.html");
None of these work. Probably I've missed a step?
c# angular asp.net-core asp.net-core-mvc
add a comment |
I have the following configuration:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseDefaultFiles();
app.UseFileServer(new FileServerOptions
FileProvider = new EmbeddedFileProvider(typeof(Startup).Assembly, typeof(Startup).Namespace + ".WebApp"),
RequestPath = string.Empty
);
app.UseCors("CorsPolicy");
app.UseMvc(routes =>
routes.MapRoute(
name: "angular",
defaults: new controller = "Angular", action = "Index",
template: "*url");
);
My Angular project files are in the namespace MyNamespace.WebApp
.
My AngularController
and Startup
classes are in the namespace MyNamespace
When I don't use MVC and access http://localhost:8000 it loads the index.html
file in the WebApp
folder. Now for all other requests (for example /action
) I have mapped it to the AngularController
, which looks as follows:
public class AngularController : Controller
public IActionResult Index()
return View("/index.html");
I have debugged and verified that the request does come to AngularController.Index()
and returns View("/index.html")
. But after that I get a 500 Error. I'm guessing because it cannot find the view file.
How do I let MVC know to fetch the index.html
file from the embedded files?
I've tried the following:
return View("~/index.html");
return View("index.html");
return View("~/../index.html");
return View("../index.html");
return View("~/WebApp/index.html");
return View("WebApp/index.html");
None of these work. Probably I've missed a step?
c# angular asp.net-core asp.net-core-mvc
add a comment |
I have the following configuration:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseDefaultFiles();
app.UseFileServer(new FileServerOptions
FileProvider = new EmbeddedFileProvider(typeof(Startup).Assembly, typeof(Startup).Namespace + ".WebApp"),
RequestPath = string.Empty
);
app.UseCors("CorsPolicy");
app.UseMvc(routes =>
routes.MapRoute(
name: "angular",
defaults: new controller = "Angular", action = "Index",
template: "*url");
);
My Angular project files are in the namespace MyNamespace.WebApp
.
My AngularController
and Startup
classes are in the namespace MyNamespace
When I don't use MVC and access http://localhost:8000 it loads the index.html
file in the WebApp
folder. Now for all other requests (for example /action
) I have mapped it to the AngularController
, which looks as follows:
public class AngularController : Controller
public IActionResult Index()
return View("/index.html");
I have debugged and verified that the request does come to AngularController.Index()
and returns View("/index.html")
. But after that I get a 500 Error. I'm guessing because it cannot find the view file.
How do I let MVC know to fetch the index.html
file from the embedded files?
I've tried the following:
return View("~/index.html");
return View("index.html");
return View("~/../index.html");
return View("../index.html");
return View("~/WebApp/index.html");
return View("WebApp/index.html");
None of these work. Probably I've missed a step?
c# angular asp.net-core asp.net-core-mvc
I have the following configuration:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseDefaultFiles();
app.UseFileServer(new FileServerOptions
FileProvider = new EmbeddedFileProvider(typeof(Startup).Assembly, typeof(Startup).Namespace + ".WebApp"),
RequestPath = string.Empty
);
app.UseCors("CorsPolicy");
app.UseMvc(routes =>
routes.MapRoute(
name: "angular",
defaults: new controller = "Angular", action = "Index",
template: "*url");
);
My Angular project files are in the namespace MyNamespace.WebApp
.
My AngularController
and Startup
classes are in the namespace MyNamespace
When I don't use MVC and access http://localhost:8000 it loads the index.html
file in the WebApp
folder. Now for all other requests (for example /action
) I have mapped it to the AngularController
, which looks as follows:
public class AngularController : Controller
public IActionResult Index()
return View("/index.html");
I have debugged and verified that the request does come to AngularController.Index()
and returns View("/index.html")
. But after that I get a 500 Error. I'm guessing because it cannot find the view file.
How do I let MVC know to fetch the index.html
file from the embedded files?
I've tried the following:
return View("~/index.html");
return View("index.html");
return View("~/../index.html");
return View("../index.html");
return View("~/WebApp/index.html");
return View("WebApp/index.html");
None of these work. Probably I've missed a step?
c# angular asp.net-core asp.net-core-mvc
c# angular asp.net-core asp.net-core-mvc
edited Nov 16 '18 at 9:36
Neil Patrao
asked Nov 16 '18 at 9:30
Neil PatraoNeil Patrao
2,37132538
2,37132538
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In this example, I've tried to create an HTML file with the name htmlpage.html
in the path Views/Home/htmlpage.html
.
In the file Index.cshtml
(path: Views/Home/Index.cshtml
):
@using Microsoft.AspNetCore.Hosting
@using System.IO
@inject IHostingEnvironment environment
@
string htmlPath = "Views/Home/htmlpage.html";
@Html.Raw(File.ReadAllText(System.IO.Path.Combine(environment.ContentRootPath, htmlPath)))
In the file htmlpage.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>bar</title>
</head>
<body>
<h3>foo</h3>
</body>
</html>
Test:
This way doesn't require: app.UseDefaultFiles();
and app.UseFileServer(...);
Sorry, but this answer is also for physical files present in the bin folder and not for "Embedded Files"
– Neil Patrao
Mar 21 at 12:35
add a comment |
My solution was to configure a controller that would use an EmbeddedFileProvider
and fetch the contents of the embedded file as follows:
public AngularController: Controller
private IAppSettings appSettings;
public AngularController(IAppSettings appSettings)
this.appSettings = appSettings;
public IActionResult Index()
var fileProvider = new EmbeddedFileProvider(this.appSettings.WebAssembly, this.appSettings.WebNamespace);
var contents = this.fileProvider.GetDirectoryContents(string.Empty);
IFileInfo index = null;
foreach (var file in contents)
if (file.Name.Equals("index.html"))
index = file;
break;
if (index == null)
throw new Exception("'index.html' not found");
var reader = new StreamReader(index.CreateReadStream());
var text = reader.ReadToEnd();
return this.Content(text, "text/html");
The Assembly
and Namespace
of the embedded files are provided using dependency injection to make it more generic. You could also hard code it into the controller.
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334937%2fhow-to-return-an-embedded-static-html-file-from-an-asp-net-core-controller%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In this example, I've tried to create an HTML file with the name htmlpage.html
in the path Views/Home/htmlpage.html
.
In the file Index.cshtml
(path: Views/Home/Index.cshtml
):
@using Microsoft.AspNetCore.Hosting
@using System.IO
@inject IHostingEnvironment environment
@
string htmlPath = "Views/Home/htmlpage.html";
@Html.Raw(File.ReadAllText(System.IO.Path.Combine(environment.ContentRootPath, htmlPath)))
In the file htmlpage.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>bar</title>
</head>
<body>
<h3>foo</h3>
</body>
</html>
Test:
This way doesn't require: app.UseDefaultFiles();
and app.UseFileServer(...);
Sorry, but this answer is also for physical files present in the bin folder and not for "Embedded Files"
– Neil Patrao
Mar 21 at 12:35
add a comment |
In this example, I've tried to create an HTML file with the name htmlpage.html
in the path Views/Home/htmlpage.html
.
In the file Index.cshtml
(path: Views/Home/Index.cshtml
):
@using Microsoft.AspNetCore.Hosting
@using System.IO
@inject IHostingEnvironment environment
@
string htmlPath = "Views/Home/htmlpage.html";
@Html.Raw(File.ReadAllText(System.IO.Path.Combine(environment.ContentRootPath, htmlPath)))
In the file htmlpage.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>bar</title>
</head>
<body>
<h3>foo</h3>
</body>
</html>
Test:
This way doesn't require: app.UseDefaultFiles();
and app.UseFileServer(...);
Sorry, but this answer is also for physical files present in the bin folder and not for "Embedded Files"
– Neil Patrao
Mar 21 at 12:35
add a comment |
In this example, I've tried to create an HTML file with the name htmlpage.html
in the path Views/Home/htmlpage.html
.
In the file Index.cshtml
(path: Views/Home/Index.cshtml
):
@using Microsoft.AspNetCore.Hosting
@using System.IO
@inject IHostingEnvironment environment
@
string htmlPath = "Views/Home/htmlpage.html";
@Html.Raw(File.ReadAllText(System.IO.Path.Combine(environment.ContentRootPath, htmlPath)))
In the file htmlpage.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>bar</title>
</head>
<body>
<h3>foo</h3>
</body>
</html>
Test:
This way doesn't require: app.UseDefaultFiles();
and app.UseFileServer(...);
In this example, I've tried to create an HTML file with the name htmlpage.html
in the path Views/Home/htmlpage.html
.
In the file Index.cshtml
(path: Views/Home/Index.cshtml
):
@using Microsoft.AspNetCore.Hosting
@using System.IO
@inject IHostingEnvironment environment
@
string htmlPath = "Views/Home/htmlpage.html";
@Html.Raw(File.ReadAllText(System.IO.Path.Combine(environment.ContentRootPath, htmlPath)))
In the file htmlpage.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>bar</title>
</head>
<body>
<h3>foo</h3>
</body>
</html>
Test:
This way doesn't require: app.UseDefaultFiles();
and app.UseFileServer(...);
answered Nov 17 '18 at 7:25
FooFoo
1
1
Sorry, but this answer is also for physical files present in the bin folder and not for "Embedded Files"
– Neil Patrao
Mar 21 at 12:35
add a comment |
Sorry, but this answer is also for physical files present in the bin folder and not for "Embedded Files"
– Neil Patrao
Mar 21 at 12:35
Sorry, but this answer is also for physical files present in the bin folder and not for "Embedded Files"
– Neil Patrao
Mar 21 at 12:35
Sorry, but this answer is also for physical files present in the bin folder and not for "Embedded Files"
– Neil Patrao
Mar 21 at 12:35
add a comment |
My solution was to configure a controller that would use an EmbeddedFileProvider
and fetch the contents of the embedded file as follows:
public AngularController: Controller
private IAppSettings appSettings;
public AngularController(IAppSettings appSettings)
this.appSettings = appSettings;
public IActionResult Index()
var fileProvider = new EmbeddedFileProvider(this.appSettings.WebAssembly, this.appSettings.WebNamespace);
var contents = this.fileProvider.GetDirectoryContents(string.Empty);
IFileInfo index = null;
foreach (var file in contents)
if (file.Name.Equals("index.html"))
index = file;
break;
if (index == null)
throw new Exception("'index.html' not found");
var reader = new StreamReader(index.CreateReadStream());
var text = reader.ReadToEnd();
return this.Content(text, "text/html");
The Assembly
and Namespace
of the embedded files are provided using dependency injection to make it more generic. You could also hard code it into the controller.
add a comment |
My solution was to configure a controller that would use an EmbeddedFileProvider
and fetch the contents of the embedded file as follows:
public AngularController: Controller
private IAppSettings appSettings;
public AngularController(IAppSettings appSettings)
this.appSettings = appSettings;
public IActionResult Index()
var fileProvider = new EmbeddedFileProvider(this.appSettings.WebAssembly, this.appSettings.WebNamespace);
var contents = this.fileProvider.GetDirectoryContents(string.Empty);
IFileInfo index = null;
foreach (var file in contents)
if (file.Name.Equals("index.html"))
index = file;
break;
if (index == null)
throw new Exception("'index.html' not found");
var reader = new StreamReader(index.CreateReadStream());
var text = reader.ReadToEnd();
return this.Content(text, "text/html");
The Assembly
and Namespace
of the embedded files are provided using dependency injection to make it more generic. You could also hard code it into the controller.
add a comment |
My solution was to configure a controller that would use an EmbeddedFileProvider
and fetch the contents of the embedded file as follows:
public AngularController: Controller
private IAppSettings appSettings;
public AngularController(IAppSettings appSettings)
this.appSettings = appSettings;
public IActionResult Index()
var fileProvider = new EmbeddedFileProvider(this.appSettings.WebAssembly, this.appSettings.WebNamespace);
var contents = this.fileProvider.GetDirectoryContents(string.Empty);
IFileInfo index = null;
foreach (var file in contents)
if (file.Name.Equals("index.html"))
index = file;
break;
if (index == null)
throw new Exception("'index.html' not found");
var reader = new StreamReader(index.CreateReadStream());
var text = reader.ReadToEnd();
return this.Content(text, "text/html");
The Assembly
and Namespace
of the embedded files are provided using dependency injection to make it more generic. You could also hard code it into the controller.
My solution was to configure a controller that would use an EmbeddedFileProvider
and fetch the contents of the embedded file as follows:
public AngularController: Controller
private IAppSettings appSettings;
public AngularController(IAppSettings appSettings)
this.appSettings = appSettings;
public IActionResult Index()
var fileProvider = new EmbeddedFileProvider(this.appSettings.WebAssembly, this.appSettings.WebNamespace);
var contents = this.fileProvider.GetDirectoryContents(string.Empty);
IFileInfo index = null;
foreach (var file in contents)
if (file.Name.Equals("index.html"))
index = file;
break;
if (index == null)
throw new Exception("'index.html' not found");
var reader = new StreamReader(index.CreateReadStream());
var text = reader.ReadToEnd();
return this.Content(text, "text/html");
The Assembly
and Namespace
of the embedded files are provided using dependency injection to make it more generic. You could also hard code it into the controller.
answered Mar 21 at 12:51
Neil PatraoNeil Patrao
2,37132538
2,37132538
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334937%2fhow-to-return-an-embedded-static-html-file-from-an-asp-net-core-controller%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown