如何使用ASP.NET Core MVC发出对HTML文件的获取请求 [英] How to make a get request for an HTML file using ASP.NET Core MVC

查看:410
本文介绍了如何使用ASP.NET Core MVC发出对HTML文件的获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个ASP.NET Core MVC项目.我设置了HomeController,设置了Homes和Shared文件夹的Views文件夹,在其中包含了适当的.cshtml文件(例如Index.cshtml,_Layout.cshtml和_ViewStart.cshtml).我还有一个PartialViews文件夹,其中包含一堆HTML文件(不是.cshtml文件).

I am making an ASP.NET Core MVC project. I have a HomeController set up, I have the Views folder with the Home and Shared folders set up, and inside those I have the appropriate .cshtml files (such as Index.cshtml, _Layout.cshtml, and _ViewStart.cshtml). I also have a PartialViews folder which has a bunch of HTML files (not .cshtml files).

我要做的就是从浏览器向PartialViews文件夹中的HTML文件发出Ajax获取请求(请注意,该文件的路径为MyProject \ Areas \ MyArea \ PartialViews \ SomeDoc.html).我知道,如果此HTML文件位于wwwroot文件夹中(保留了静态文件,我也正在使用该文件夹),则可以向url'localhost/SomeDoc.html'发出get请求.但是它不在wwwroot下,所以我想我必须经过在MyArea中定义的HomeController吗?我不确定如何格式化get请求,以便HomeController.Index()中的return View()知道要获取并返回指定文件夹路径中的html文件.我已经尝试过以下网址:"localhost/MyArea/Home/PartialViews/SomeDoc.html",尽管它只会返回404.

All I want to do is make an Ajax get request from the browser for an HTML file in the PartialViews folder (which, just to be clear, has the path MyProject\Areas\MyArea\PartialViews\SomeDoc.html). I know that if this HTML file were in the wwwroot folder (where static files are kept -- I am also using this) I could just make a get request to the url 'localhost/SomeDoc.html'. But it's not under wwwroot, so I guess I have to go through the HomeController I defined in MyArea? I'm not sure how to format the get request so that return View() in HomeController.Index() knows to fetch and return the html file in the specified folder path. I've tried this url: 'localhost/MyArea/Home/PartialViews/SomeDoc.html', though that just returns a 404.

ASP.NET Core 与MVC有关的文档除非我误解了它们,否则似乎并没有真正解决这个问题.我不确定的另一件事是是否应该像本文介绍了将部分视图与MVC结合使用.我很困惑,因为文章(a)仅引用.cshtml文件(而我只有html文件),而(b)没有解释如何格式化视图请求(除非再次,我遗漏了一些内容) ).

ASP.NET Core docs pertaining to MVC don't really seem to address this unless I'm misunderstanding them. Another thing I'm not sure about is whether these HTML files in this PartialViews folder are supposed to be used like this article on using partial views with MVC describes. I'm confused because the article (a) only references .cshtml files (whereas I just have html files), and (b) doesn't explain how to format the request for the view (unless, again, I'm missing something).

推荐答案

似乎您需要花更多时间在文档上.默认情况下,唯一的公共服务目录是wwwroot.如果您希望静态文件中间件也可以提供另一个目录,则需要对其进行配置.但是,直接为您的Views目录提供服务完全是100%不适当的,所以不要这样做.

It seems you need to spend some more time with docs. The only publicly served directory by default is wwwroot. If you want another directory to be served as well by the static files middleware, you need to configure that. However, it would be totally 100% inappropriate to serve your Views directory directly, so don't do that.

如果您希望能够请求静态HTML文件,则可以在wwwroot下添加它们,或为此目的创建一个完全不同的目录,并通过静态文件中间件来提供服务.

If you want to be able to request static HTML files, add them under wwwroot or create a totally different directory just for that purpose and serve that as well via the static files middleware.

但是,通常来说,静态HTML并不太有用,如果您确实需要静态HTML,它就会提出一个问题,即为什么它不只是视图的一部分.正常路径是具有.cshtml视图,ASP.NET Core会将这些视图呈现为HTML,作为返回响应的请求管道的一部分.

Typically, though, static HTML isn't overly useful, and if you truly need static HTML, it begs the question of why it's just not part of the view to begin with. The normal path is to have .cshtml views, which ASP.NET Core will render to HTML as part of the request pipeline to return the response.

简单地说,您只需创建一个返回PartialView的动作,并传入您想返回的.cshtml文件的名称以及模型(如果适用).然后,在您的JS中,对绑定到该动作的路由进行AJAX请求,然后将部分视图重新呈现为HTML.

Simply, you just create an action that returns PartialView and pass in the name of the .cshtml file you want to return, along with a model, if applicable. In your JS, then, you do an AJAX request to the route bound to that action, and you get back your partial view rendered into HTML.

这篇关于如何使用ASP.NET Core MVC发出对HTML文件的获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆