Tag: asp.net core mvc

将IHtmlContent / TagBuilder转换为C#中的字符串

我正在使用ASP.NET 5.我需要将IHtmlContent转换为String IIHtmlContent是ASP.NET 5 Microsoft.AspNet.Html.Abstractions命名空间的一部分,是TagBuilder实现的接口 简化我有以下方法 public static IHtmlContent GetContent() { return new HtmlString(“blah”); } 当我参考它 string output = GetContent().ToString(); 我得到GetContent()的以下输出 “Microsoft.AspNet.Mvc.Rendering.TagBuilder” 并不是 blah 我想要的 我也尝试过使用StringBuilder StringBuilder html = new StringBuilder(); html.Append(GetContent()); 但它也会附加相同的命名空间而不是字符串值 我试着把它投射到TagBuilder TagBuilder content = (TagBuilder)GetContent(); 但TagBuilder没有转换为字符串的方法 如何将IHtmlContent或TagBuilder转换为字符串?

在ASP.NET核心中注入IUrlHelper

在RC1中 , IUrlHelper可以在服务中注入services.AddMvc()在启动类中使用services.AddMvc() ) 这在RC2中不再起作用。 有没有人知道如何在RC2中做到这一点,因为刚刚新建一个UrlHelper需要一个ActionContext对象。 不知道如何在控制器之外得到它。

在ASP.NET 5中使用MimeMapping(vNext)

我正在尝试将我的旧mvc5项目移动到mvc6。 旧代码是: public string ContentType { get { if (!string.IsNullOrEmpty(FileName)) return MimeMapping.GetMimeMapping(FileName); return null; } } 错误是 当前上下文中不存在名称“MimeMapping”

从ASP.NET 5控制器VS 2015获取wwwroot文件夹路径

很抱歉有一个noob问题,但似乎我无法从Controller获取Server.MapPath。 我需要从wwwroot的images文件夹输出json文件列表。 他们是在wwwroot / images。 我怎样才能获得可靠的wwwroot路径? using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNet.Mvc; using www.Classes; using System.Web; namespace www.Controllers { [Route(“api/[controller]”)] public class ProductsController : Controller { [HttpGet] public IEnumerable Get() { FolderScanner scanner = new FolderScanner(Server.MapPath(“/”)); return scanner.scan(); } } } System.MapPath似乎在System.Web命名空间中不可用。 Project正在使用ASP.NET 5和dotNET 4.6 Framework

在MVC 6中,如何在视图中编码复选框列表并将选中的值传递给控制器​​?

抱歉,我的大多数搜索都会将我带到旧的MVC代码。 任何帮助将不胜感激。 在带有标记帮助器的MVC 6中,如何编写一组复选框: 使用标签帮助器作为标签,因此单击它将切换选中的值 将选中的值保存(绑定?)到IsOptionSelected属性 单击“提交”后,将这些选中的值传递回Controller ? 我能够正确显示带有标签的复选框,但我不知道如何通过模型将检查的值传递回控制器。 现在,IsOptionSelected值返回false。 我还能够为标签工作制作html帮助器,但不能为标签帮助器制作。 我可能也编码这些都错了所以任何提示都会有所帮助! 这是我到目前为止所拥有的: 显示: 实体: public class PhoneOption { public bool IsOptionSelected { get; set; } = false; public int OptionId { get; set; } public string OptionName { get; set; } } 模型: [Display(Name = “Phone Options”)] public IEnumerable PhoneOptions { get; set; } […]

使用IConfigurationRoot将应用程序的连接字符串传递到ASP.NET 5中的存储库类库

我有一个ASP.NET 5 MVC Web应用程序,在Startup.cs中我看到了公共属性 IConfigurationRoot Configuration 正被设置为builder.Build(); 在整个MVC Web应用程序中,我可以做到 Startup.Configuration[“Data:DefaultConnection:ConnectionString”] 从appsettings.json文件中获取conn字符串。 如何使用构造函数注入将ASP.NET 5 MVC appsettings.json指定的连接字符串传递给我的存储库类库? 更新: 这是所有其他存储库inheritance的基本存储库(正如您所看到的,我现在在这里有一个硬编码连接字符串): public class BaseRepo { public static string ConnectionString = “Server=MYSERVER;Database=MYDATABASE;Trusted_Connection=True;”; public static SqlConnection GetOpenConnection() { var cs = ConnectionString; var connection = new SqlConnection(cs); connection.Open(); return connection; } } 在我的appsettings.json文件中的asp.net 5 Web应用程序中,我有以下内容,相当于将连接字符串添加到.net 4.5 webapp中的web.config: “Data”: { “DefaultConnection”: { […]

如何在ASP.NET Core中解析ConfigureServices中的实例

是否可以从Startup中的ConfigureServices方法解析IOptions的实例? 通常,您可以使用IServiceProvider初始化实例,但在注册服务时此阶段没有它。 public void ConfigureServices(IServiceCollection services) { services.Configure( configuration.GetConfigurationSection(nameof(AppSettings))); // How can I resolve IOptions here? }

如何正确地在MVC6中注入HttpContext

我的API中的数据服务层需要httpcontext中请求的信息,我读了这个问题 ,他们说我应该使用ActionContext而不是HttpContext.Current(在MVC6中停止)。 第一种方法是通过重写此方法来设置控制器内的数据: public void OnActionExecuting(ActionExecutingContext context) { var routeData = context.RouteData; var httpContext = context.HttpContext; … } 或者通过注入服务层使用DI public MyService(IContextAccessor contextAccessor) { _httpContext = contextAccessor.Value.HttpContext; _routeData = contextAccessor.Value.RouteData; } 但我不确定下面列出的这两行代码是否正确 services.AddTransient<IContextAccessor,ContextAccessor>(); services.AddTransient<IContextAccessor>(); 当我这样做时,我得到了这个错误。 尝试激活时无法解析“Microsoft.AspNet.Mvc.ActionContext”类型的服务 更新 project.json web项目 “DIMultiTenan.Infrastructure”: “”, “DIMultiTenan.MongoImplementation”: “”, “Microsoft.AspNet.Server.IIS”: “1.0.0-beta3”, “Microsoft.AspNet.Mvc”: “6.0.0-beta3”, “Microsoft.AspNet.StaticFiles”: “1.0.0-beta3”, “Microsoft.AspNet.Server.WebListener”: “1.0.0-beta3”

如何对ASP.NET Core控制器或模型对象进行unit testing?

我试图在Visual Studio 2015中使用ASP.NET Core MVC(预览期间的ASP.NET 5,现在称为ASP.Net Core)应用程序在unit testing下获得一些控制器,模型和存储库(数据访问)C#类。 我有以下结构: Solution | src | |– ITConsole <- main app (ASP.NET MVC, DNX 4.5.1) | `– ITConsoleTests <- What kind of project should this be? MainApp使用的是DNX 4.5.1,但似乎如果我创建一个标准的nUnit Unit测试应用程序,它只能用作经典的.NET Framework类库,目标是.NET Framework 4.5.2,而不是Web类库。可以使用我的主应用程序。 因此,为了防止它可能作为经典的.NET框架Microsoftunit testing框架项目(.net程序集)工作,我尝试手动查找和添加引用(通过添加引用和浏览)来获取要解析的.NET依赖项。 我知道.NET程序集引用很遗憾是不可传递的。 因此,如果UnitTest.dll具有对MainApp.dll的引用,并且MainApp.dll依赖于ASP.NET MVC以及它所依赖的所有其他内容,那么我必须自己执行此操作。 这就是我想要做的。 我将C:\dev\Demo\ITConsole\artifacts\bin\ITConsole\Debug\dnx451\ITConsole.dll到我的unit testing项目中,这样我就可以开始编译代码了。 unit testing类编译但它们不运行,可能是因为尝试添加对ASP.NET的引用。 现在,即使我添加了对Common.Logging.Core和Common.Logging的引用,当我在Test explorer上单击“全部运行”时,我收到此错误: Test Name: TestStudyLogReadDocument Test […]

MVC6国家下拉列表

我正在尝试使用MVC6 Tag Helpers创建CountryCode和CountryName的下拉列表,以便用户在注册后可以选择他们的国家/地区。 到目前为止,视图的相关部分看起来像这样 viewmodel的相关部分看起来像这样 [Display(Name = “Country”)] public string CountryCode { get; set; } public IEnumerable Countries { get; set; } 国家看起来像这样 public partial class Country { [Key] public string CountryCode { get; set; } public string CountryName { get; set; } public virtual ICollection Users { get; set; } } 控制器将视图模型返回国家/地区列表 var model […]