Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。

7
Moq模拟扩展方法
我已有一个接口... public interface ISomeInterface { void SomeMethod(); } 我已经使用mixin扩展了这个界面 public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface someInterface) { // Implementation here } } 我有一个类,这就是我想测试的。 public class Caller { private readonly ISomeInterface someInterface; public Caller(ISomeInterface someInterface) { this.someInterface = someInterface; } public void Main() { someInterface.AnotherMethod(); } } …

11
LINQ包含不区分大小写的
该代码区分大小写,如何使其不区分大小写? public IQueryable<FACILITY_ITEM> GetFacilityItemRootByDescription(string description) { return this.ObjectContext.FACILITY_ITEM.Where(fi => fi.DESCRIPTION.Contains(description)); }
173 c#  linq 

13
从控制器内部获取控制器和动作名称?
对于我们的Web应用程序,我需要保存获取和显示的项目的顺序,具体取决于视图-确切地说,是生成视图的控制器和操作(当然还有用户ID,但这不是重点)。 我认为不仅要在每个控制器动作中自己给出一个标识符(以便将其用于与视图有关的数据库输出排序),我还认为,从其获取的控制器和动作方法中自动创建该标识符会更安全,更轻松。从。 如何从控制器的action方法中获取控制器的名称和动作?还是我需要反思?我想这很容易,谢谢您!

11
防止在Web API中序列化属性
我正在使用MVC 4 Web API和ASP.NET Web Forms 4.0来构建Rest API。运作良好: [HttpGet] public HttpResponseMessage Me(string hash) { HttpResponseMessage httpResponseMessage; List<Something> somethings = ... httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, new { result = true, somethings = somethings }); return httpResponseMessage; } 现在,我需要防止某些属性被序列化。我知道我可以在列表上使用一些LINQ并仅获取我需要的属性,通常这是一个好方法,但是在当前情况下,something对象太复杂了,并且我需要使用不同方法的一组不同属性,因此易于在运行时标记每个要忽略的属性。 有没有办法做到这一点?

4
C#短/长/整数文字格式?
在C / C#等中,您可以告诉编译器文字数字不是它看起来的样子(即,float代替double,unsigned long而不是int: var d = 1.0; // double var f = 1.0f; // float var u = 1UL; // unsigned long 等等 有人可以指出我的清单吗?我正在寻找short或的后缀Int16。

9
将其他ViewData传递到强类型的局部视图
我有一个采用ProductImage的强类型部分视图,并且在呈现它时,我还想为其提供一些其他ViewData,这些ViewData是我在包含页面中动态创建的。如何通过RenderPartial调用将强类型对象和自定义ViewData传递到部分视图? var index = 0; foreach (var image in Model.Images.OrderBy(p => p.Order)) { Html.RenderPartial("ProductImageForm", image); // < Pass 'index' to partial index++; }

4
为什么XML-Serializable类需要无参数构造函数
我正在编写代码以进行Xml序列化。具有以下功能。 public static string SerializeToXml(object obj) { XmlSerializer serializer = new XmlSerializer(obj.GetType()); using (StringWriter writer = new StringWriter()) { serializer.Serialize(writer, obj); return writer.ToString(); } } 如果参数是没有无参数构造函数的类的实例,则它将引发异常。 未处理的异常:System.InvalidOperationException:CSharpConsole.Foo无法序列化,因为它没有无参数的构造函数。在System.Xml.Serialization.TypeScope.GetTypeDesc(Type type,MemberInfo源,Boolean directReference,Boolean throwOnError)在System.Xml.Serialization.ModelScope.GetTypeModel(Type type, System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type,XmlRootAttribute root,String defaultNamespace)在System.Xml.Serialization.XmlSerializer..ctor(Type type,String defaultName space)在System.Xml.Serialization。 XmlSerializer..ctor(类型类型) 为什么必须有一个无参数的构造函数才能使xml序列化成功? 编辑:感谢cfeduke的回答。无参数构造函数可以是私有的或内部的。

7
ASP.NET Core表单POST导致HTTP 415不支持的媒体类型响应
将表单POST HTTP请求(Content-Type: application/x-www-form-urlencoded)发送到以下控制器将导致HTTP 415不支持的媒体类型响应。 public class MyController : Controller { [HttpPost] public async Task<IActionResult> Submit([FromBody] MyModel model) { //... } } 表单发布HTTP标头: POST /submit HTTP/1.1 Host: example.com:1337 Connection: keep-alive Content-Length: 219 Pragma: no-cache Cache-Control: no-cache Origin: https://example.com:1337 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) …

5
字符串中的双引号转义
双引号可以这样转义: string test = @"He said to me, ""Hello World"". How are you?"; 但这涉及"在字符串中添加字符。是否有C#函数或其他方法来转义双引号,以便不需要更改字符串?
173 c#  string  double-quotes 

5
无法在Lambda表达式中使用ref或out参数
为什么不能在lambda表达式中使用ref或out参数? 我今天遇到了错误,找到了解决方法,但是我仍然很好奇为什么这是编译时错误。 CS1628:无法在匿名方法,lambda表达式或查询表达式中使用ref或out参数'parameter' 这是一个简单的例子: private void Foo() { int value; Bar(out value); } private void Bar(out int value) { value = 3; int[] array = { 1, 2, 3, 4, 5 }; int newValue = array.Where(a => a == value).First(); }
173 c#  lambda 

7
如何将字符串转换为其等效的LINQ表达式树?
这是原始问题的简化版本。 我有一个叫Person的课: public class Person { public string Name { get; set; } public int Age { get; set; } public int Weight { get; set; } public DateTime FavouriteDay { get; set; } } ...然后说一个实例: var bob = new Person { Name = "Bob", Age = 30, Weight = …
173 c#  lambda  antlr  dsl  predicate 

6
如何在ASP.NET WebAPI中返回文件(FileContentResult)
在常规MVC控制器中,我们可以使用来输出pdf FileContentResult。 public FileContentResult Test(TestViewModel vm) { var stream = new MemoryStream(); //... add content to the stream. return File(stream.GetBuffer(), "application/pdf", "test.pdf"); } 但是如何将其更改为ApiController? [HttpPost] public IHttpActionResult Test(TestViewModel vm) { //... return Ok(pdfOutput); } 这是我尝试过的方法,但似乎不起作用。 [HttpGet] public IHttpActionResult Test() { var stream = new MemoryStream(); //... var content = new …

8
如何编辑.csproj文件
当我使用.NET Framework 4.0 MSBUILD.EXE文件编译.csproj文件时,出现“ website01.csproj”当前上下文中找不到错误“ lable01” 实际上,我需要添加每个ASP.NET页及其“代码隐藏”文件的引用,我已经做到了,它可以正常工作,但是上面的错误尚未解决。 我希望这意味着我需要在该.csproj文件中添加表单名称“ LABLE01”,但我不知道其语法。任何人都请为我提供在.csproj文件中添加表单名称的语法。
173 c#  .net  asp.net 

9
抛出HttpResponseException或返回Request.CreateErrorResponse吗?
在阅读了ASP.NET Web API中的异常处理文章之后,我对何时引发异常与返回错误响应感到困惑。我还想知道当您的方法返回特定于域的模型而不是HttpResponseMessage... 时是否可以修改响应... 因此,回顾一下我的问题,后面是一些带有案例编号的代码: 问题 有关案例1的问题 我是否应该始终使用HttpResponseMessage而不是具体的域模型,以便可以自定义消息? 如果要返回具体域模型,可以自定义消息吗? 有关案例#2、3、4的问题 我应该抛出异常还是返回错误响应?如果答案是“视情况而定”,那么您能否举例说明何时使用另一种方法。 HttpResponseExceptionvs与thing有Request.CreateErrorResponse什么区别?输出到客户端似乎相同... 我是否应该总是使用HttpError“包装”错误中的响应消息(是否抛出异常或返回错误响应)? 案例样本 // CASE #1 public Customer Get(string id) { var customer = _customerService.GetById(id); if (customer == null) { var notFoundResponse = new HttpResponseMessage(HttpStatusCode.NotFound); throw new HttpResponseException(notFoundResponse); } //var response = Request.CreateResponse(HttpStatusCode.OK, customer); //response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300)); return …

15
AppSettings从.config文件获取值
我无法访问配置文件中的值。 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var clientsFilePath = config.AppSettings.Settings["ClientsFilePath"].Value; // the second line gets a NullReferenceException .config文件: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <!-- ... --> <add key="ClientsFilePath" value="filepath"/> <!-- ... --> </appSettings> </configuration> 您有什么建议吗?

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.