简体   繁体   English

c#WCF REST强类型WebGet对象用于参数和验证

[英]c# WCF REST strongly typed WebGet object for parameters and validation

Can you do the below in WCF 4.0 Rest like you can in ASP.NET MVC? 您可以像在ASP.NET MVC中一样在WCF 4.0 Rest中执行以下操作吗?

In ASP.NET MVC I can create a strongly typed object commonly known as a ViewModel to handle error validation. 在ASP.NET MVC中,我可以创建一个强类型对象(通常称为ViewModel)来处理错误验证。

Instead of the following: 代替以下内容:

public ActionResult SomeAction(string firstname, string lastname, string address, int phone)

I could have the following: 我可以有以下内容:

public ActionResult SomeAction(UserObject obj)

Where UserObject is defined as: 其中UserObject定义为:

public class UserObject
{
   [Required(ErrorMessage = "firstname is a required paramater")]
   public string firstname { get; set; }
   [StringLength(50, ErrorMessage = "lastname is too long")]
   public string lastname { get; set; }
   [StringLength(160)]
   public string address { get; set; }
   public int phone { get; set; }
}

What I am basically wanting to do is create the parameters in a strongly typed object and have my error messages there. 我基本上想要做的是在一个强类型的对象中创建参数,并在其中存储我的错误消息。 I could then format the error message as xml and return it to the user. 然后,我可以将错误消息格式化为xml并将其返回给用户。

So in WCF REST. 因此在WCF REST中。 Instead of My method looking like: 而不是我的方法看起来像:

[WebGet]
public IEnumerable<ObjectResult> SomeAction(string firstname, string lastname, string address, int phone)

I want the following: 我想要以下内容:

[WebGet]
public IEnumerable<ObjectResult> SomeAction(UserObject obj)

Is this possible in WCF REST 4.0? 在WCF REST 4.0中可能吗?

Default WCF is not able to do that. 默认WCF无法执行此操作。 You must create custom behavior with custom implementation of IDispatchMessageFormatter to collect parameters from query string and build the object. 您必须使用IDispatchMessageFormatter自定义实现来创建自定义行为,以从查询字符串中收集参数并构建对象。 Here is an example how to build such behavior and formatter. 这是一个如何构建这种行为和格式化程序的示例 It would be like if you have to write custom model binder for each custom ViewModel in ASP.NET MVC. 就像您必须为ASP.NET MVC中的每个自定义ViewModel编写自定义模型绑定程序一样。

Btw. 顺便说一句。 there is also no build in logic which would simply allow you to call validation (like Model.IsValid in MVC). 也没有内置的逻辑可以简单地调用验证(例如MVC中的Model.IsValid )。 You will need to use infrastructure classes used with data annotations manually ( System.ComponentModel.DataAnnotations.Validator ). 您将需要手动使用与数据注释一起使用的基础结构类( System.ComponentModel.DataAnnotations.Validator )。

C# 强类型ConfigurationBuilder列表<object>从应用设置<div id="text_translate"><p>真的在为这个苦苦挣扎——它应该这么难吗?!</p><p> 我的 appsettings 中有一个简单的 object 数组:</p><pre> "AppSettings": { "Names": [ { "Id": "1", "Name": "Mike" }, { "Id": "2", "Name": "John" } ] }</pre><p> 然后我有一个 class</p><pre> public class AppSettings { public List&lt;Names&gt; Names { get; set; } = new List&lt;Names&gt;(); } public class Names { public string Id { get; set; } public string Name { get; set; } }</pre><p> 我在我的应用设置中读到:</p><pre> var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json").Build(); var config = new AppSettings();</pre><p> 这就是出错的地方:</p><pre> config.Names = configuration.GetSection("AppSettings:Names") //&lt;&lt;&lt;&lt; what do I do here?</pre><p> 似乎它都与IConfigurationSection相关,这没有帮助。</p></div></object> - C# strongly typed ConfigurationBuilder List<object> from appsettings

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#强类型数据集验证方法 - C# Strongly typed dataset validation approach C# 强类型属性 - C# strongly typed properties 来自字符串类型名称的强类型对象(C#) - Strongly typed object from string type name (C#) 使用 C# 从强类型类动态构建对象? - Dynamically build an object from a strongly typed class using C#? 从通用(C#)创建强类型对象时发生编译错误 - Compile error creating strongly typed object from generic (C#) 如何使用C#从xml获取强类型对象? - How to get a strongly typed object from xml using c#? C#,将强类型对象作为元素插入XML文档 - C#, insert strongly typed object into XML Document as Element C# 强类型ConfigurationBuilder列表<object>从应用设置<div id="text_translate"><p>真的在为这个苦苦挣扎——它应该这么难吗?!</p><p> 我的 appsettings 中有一个简单的 object 数组:</p><pre> "AppSettings": { "Names": [ { "Id": "1", "Name": "Mike" }, { "Id": "2", "Name": "John" } ] }</pre><p> 然后我有一个 class</p><pre> public class AppSettings { public List&lt;Names&gt; Names { get; set; } = new List&lt;Names&gt;(); } public class Names { public string Id { get; set; } public string Name { get; set; } }</pre><p> 我在我的应用设置中读到:</p><pre> var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json").Build(); var config = new AppSettings();</pre><p> 这就是出错的地方:</p><pre> config.Names = configuration.GetSection("AppSettings:Names") //&lt;&lt;&lt;&lt; what do I do here?</pre><p> 似乎它都与IConfigurationSection相关,这没有帮助。</p></div></object> - C# strongly typed ConfigurationBuilder List<object> from appsettings c#如何dapperRow到网格视图中显示的强类型对象 - c# How to dapperRow to strongly typed object displayed in grid view 键入vs强烈键入C# - Typed vs Strongly Typed in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM