默认新建立的MVC程序中,在Views目录下,新增加了一个_GlobalImport.cshtml
文件和_ViewStart.cshtml
平级,该文件的功能类似于之前Views目录下的web.config文件,之前我们在该文件中经常设置全局导入的命名空间,以避免在每个view文件中重复使用@using xx.xx
语句。
默认的示例如下:
@using BookStore@using Microsoft.Framework.OptionsModel@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
上述代码表示,引用BookStore
和Microsoft.Framework.OptionsModel
命名空间,以及Microsoft.AspNet.Mvc.TagHelpers
程序集下的所有命名空间。
关于addTagHelper功能,我们已经在TagHelper中讲解过了
注意,在本例中,我们只引用了BookStore
命名空间,并没有引用BookStore.Controllers
命名空间,所以我们在任何视图中,都无法访问HomeController
类(也不能以Controllers.HomeController
的形式进行访问),希望微软以后能加以改进。
要获取用户访问者的IP地址相关信息,可以利用依赖注入,获取IHttpConnectionFeature
的实例,从该实例上可以获取IP地址的相关信息,实例如下:
[csharp] view plaincopyprint?
<code class="hljs cs" style="display:block; overflow-x:auto; color:rgb(0,0,0); line-height:1.5!important; font-family:'Courier New',sans-serif!important; font-size:12px!important; border:1px solid rgb(204,204,204)!important; padding:5px!important"><span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> connection1 = Request.HttpContext.GetFeature<IHttpConnectionFeature>();
<span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> connection2 = Context.GetFeature<IHttpConnectionFeature>();
<span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> isLocal = connection1.IsLocal; <span class="hljs-comment" style="color:green">//是否本地IP </span>
<span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> localIpAddress = connection1.LocalIpAddress; <span class="hljs-comment" style="color:green">//本地IP地址</span>
<span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> localPort = connection1.LocalPort; <span class="hljs-comment" style="color:green">//本地IP端口</span>
<span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> remoteIpAddress = connection1.RemoteIpAddress; <span class="hljs-comment" style="color:green">//远程IP地址</span>
<span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> remotePort = connection1.RemotePort; <span class="hljs-comment" style="color:green">//本地IP端口</span></code>
类似地,你也可以通过IHttpRequestFeature
、IHttpResponseFeature
、IHttpClientCertificateFeature
、 IWebSocketAcceptContext
等接口,获取相关的实例,从而使用该实例上的特性,上述接口都在命名空间Microsoft.AspNet.HttpFeature
的下面。
MVC6在文件上传方面,给了新的改进处理,举例如下:
[html] view plaincopyprint?
<code class="hljs xml" style="display:block; overflow-x:auto; color:rgb(0,0,0); line-height:1.5!important; font-family:'Courier New',sans-serif!important; font-size:12px!important; border:1px solid rgb(204,204,204)!important; padding:5px!important"><span class="hljs-tag" style="color:rgb(0,0,255)"><<span class="hljs-title" style="color:rgb(163,21,21)">form</span> <span class="hljs-attribute" style="color:red">method</span>=<span class="hljs-value">"post"</span> <span class="hljs-attribute" style="color:red">enctype</span>=<span class="hljs-value">"multipart/form-data"</span>></span>
<span class="hljs-tag" style="color:rgb(0,0,255)"><<span class="hljs-title" style="color:rgb(163,21,21)">input</span> <span class="hljs-attribute" style="color:red">type</span>=<span class="hljs-value">"file"</span> <span class="hljs-attribute" style="color:red">name</span>=<span class="hljs-value">"files"</span> <span class="hljs-attribute" style="color:red">id</span>=<span class="hljs-value">"files"</span> <span class="hljs-attribute" style="color:red">multiple</span> /></span>
<span class="hljs-tag" style="color:rgb(0,0,255)"><<span class="hljs-title" style="color:rgb(163,21,21)">input</span> <span class="hljs-attribute" style="color:red">type</span>=<span class="hljs-value">"submit"</span> <span class="hljs-attribute" style="color:red">value</span>=<span class="hljs-value">"submit"</span> /></span>
<span class="hljs-tag" style="color:rgb(0,0,255)"></<span class="hljs-title" style="color:rgb(163,21,21)">form</span>></span></code>
我们在前端页面定义上述上传表单,在接收可以使用MVC6中的新文件类型IFormFile
,实例如下:
[csharp] view plaincopyprint?
<code class="hljs cs" style="display:block; overflow-x:auto; color:rgb(0,0,0); line-height:1.5!important; font-family:'Courier New',sans-serif!important; font-size:12px!important; border:1px solid rgb(204,204,204)!important; padding:5px!important">[HttpPost]
<span class="hljs-function"><span class="hljs-keyword" style="color:rgb(0,0,255)">public</span> <span class="hljs-keyword" style="color:rgb(0,0,255)">async</span> Task<IActionResult> <span class="hljs-title" style="color:rgb(163,21,21)">Index</span><span class="hljs-params">(IList<IFormFile> files)</span>
</span>{
<span class="hljs-keyword" style="color:rgb(0,0,255)">foreach</span> (<span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> file <span class="hljs-keyword" style="color:rgb(0,0,255)">in</span> files)
{
<span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> fileName = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim(<span class="hljs-string" style="color:rgb(163,21,21)">'"'</span>);<span class="hljs-comment" style="color:green">// beta3版本的bug,FileName返回的字符串包含双引号,如"fileName.ext"</span>
<span class="hljs-keyword" style="color:rgb(0,0,255)">if</span> (fileName.EndsWith(<span class="hljs-string" style="color:rgb(163,21,21)">".txt"</span>))<span class="hljs-comment" style="color:green">// 只保存txt文件</span>
{
<span class="hljs-keyword" style="color:rgb(0,0,255)">var</span> filePath = _hostingEnvironment.ApplicationBasePath + <span class="hljs-string" style="color:rgb(163,21,21)">"\\wwwroot\\"</span>+ fileName;
<span class="hljs-keyword" style="color:rgb(0,0,255)">await</span> file.SaveAsAsync(filePath);
}
}
<span class="hljs-keyword" style="color:rgb(0,0,255)">return</span> RedirectToAction(<span class="hljs-string" style="color:rgb(163,21,21)">"Index"</span>);<span class="hljs-comment" st