{"id":713,"date":"2019-10-07T16:27:38","date_gmt":"2019-10-07T07:27:38","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=713"},"modified":"2019-10-07T16:30:09","modified_gmt":"2019-10-07T07:30:09","slug":"mvc-download-file","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=713","title":{"rendered":"MVC Download File and View"},"content":{"rendered":"\n<pre class=\"lang:c# decode:true \" >using Microsoft.AspNetCore.Hosting;\r\nusing Microsoft.AspNetCore.Builder;\r\nusing Microsoft.AspNetCore.Http;\r\nusing Microsoft.Extensions.Logging;\r\nusing Microsoft.Extensions.DependencyInjection;\r\nusing Microsoft.AspNetCore;\r\nusing Microsoft.Extensions.Configuration;\r\nusing Microsoft.AspNetCore.Mvc;\r\nusing System.Net.Http.Headers;\r\n\r\nnamespace StartupBasic\r\n{\r\n    public class Startup\r\n    {\r\n        public Startup(IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration)\r\n        {\r\n            \/\/These are three services available at constructor\r\n        }\r\n\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            services.AddMvcCore().\r\n                SetCompatibilityVersion(CompatibilityVersion.Version_2_1);\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration)\r\n        {\r\n            app.UseMvc(routes =&gt;\r\n                routes.MapRoute(\r\n                    name: \"default_route\",\r\n                    template: \"{controller}\/{action}\/{id?}\",\r\n                    defaults: new { controller = \"Home\", action = \"Index\" })\r\n                );\r\n        }\r\n    }\r\n\r\n    public class HomeController : Controller\r\n    {\r\n        IHostingEnvironment _env;\r\n\r\n        public HomeController(IHostingEnvironment env)\r\n        {\r\n            _env = env;\r\n        }\r\n\r\n        public ActionResult Index()\r\n        {\r\n            return new ContentResult\r\n            {\r\n                Content = \"&lt;html&gt;&lt;body&gt;&lt;h1&gt;Download File&lt;\/h1&gt;Download a copy of  &lt;a href=\\\"\/home\/hegel\\\"&gt;Hegel (pdf)&lt;\/a&gt;&lt;\/body&gt;&lt;\/html&gt;\",\r\n                ContentType = \"text\/html\"\r\n            };\r\n        }\r\n\r\n\r\n        \/\/public FileStreamResult Hegel()\r\n        \/\/{\r\n        \/\/    var pathToIdeas = System.IO.Path.Combine(_env.WebRootPath, \"hegel.pdf\");\r\n\r\n        \/\/    \/\/This is a contrite example to demonstrate returning a stream. If you have a physical file on disk, just use PhySicalFileResult that takes a path. \r\n        \/\/    return new FileStreamResult(System.IO.File.OpenRead(pathToIdeas), \"application\/pdf\")\r\n        \/\/    {\r\n        \/\/        FileDownloadName = \"hegel.pdf\"\r\n        \/\/    };\r\n        \/\/}\r\n\r\n        public PhysicalFileResult Hegel()\r\n        {\r\n            var pathToIdeas = System.IO.Path.Combine(_env.WebRootPath, \"hegel.pdf\");\r\n\r\n            return new PhysicalFileResult(pathToIdeas, \"application\/pdf\");\r\n        }\r\n    }\r\n\r\n    public class Program\r\n    {\r\n        public static void Main(string[] args)\r\n        {\r\n            CreateWebHostBuilder(args).Build().Run();\r\n        }\r\n\r\n        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;\r\n            WebHost.CreateDefaultBuilder(args)\r\n                .UseStartup&lt;Startup&gt;()\r\n                .UseEnvironment(\"Development\");\r\n    }\r\n}<\/pre>\n<p>\uacb0\uacfc :<br \/>\nDownload File<br \/>\nDownload a copy of Hegel (pdf)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Mvc; using System.Net.Http.Headers; namespace StartupBasic { public class Startup { public Startup(IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { \/\/These are three services available at constructor } public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(). SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } public void Configure(IApplicationBuilder app, IHostingEnvironment env,\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/?p=713\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[108],"tags":[],"class_list":["post-713","post","type-post","status-publish","format-standard","hentry","category-net-core"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=713"}],"version-history":[{"count":3,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/713\/revisions"}],"predecessor-version":[{"id":716,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/713\/revisions\/716"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}