简体   繁体   English

ASP.NET 和 C#:你在哪里存储 rest 客户端 Z572D4E421E5E6B20BC11D815EEA

[英]ASP.NET and C#: where do you store the rest client url?

I have my rest client url hard-coded in my code-behind, but upon peer-reviewing my code, I was asked to move that url to the config file so that it can be changed for each environment.我有我的 rest 客户端 url 硬编码在我的代码隐藏中,但是在同行评审我的代码时,我被要求移动 Z572D4E421E5E6B9BC11D815E8A0271,以便它可以更改为每个环境的配置文件。

Visual Studio 2019 now complains because my rest client url has an invalid = sign as token in the url itself, and it expects ; Visual Studio 2019 现在抱怨,因为我的 rest 客户端 url 在 url 本身中有一个无效的=符号作为令牌,它期望; as token instead.而是作为令牌。

Has anyone ever come across this, and is it correct to move the rest client to the config file?有没有人遇到过这种情况,将 rest 客户端移动到配置文件是否正确? In theory that should not change.理论上不应该改变。

Can't share the full url, but the part that is highlighted as error is this: version=2.0&details=true .无法共享完整的 url,但突出显示为错误的部分是: version=2.0&details=true

I found the answer.我找到了答案。 The problem is in the & symbol itself.问题出在&符号本身。 Once converted to &一旦转换为& , the errors Visual Studio was highlighting were gone and my solution worked again. ,Visual Studio 突出显示的错误消失了,我的解决方案再次起作用。

If i will do that i will save in config file only base url like this如果我这样做,我将像这样仅将基础 url 保存在配置文件中

  "WebConfig": {
    "SmsCenterApi": "https://some_site.com/SendService"
  }

and in code I can complete the link在代码中我可以完成链接

string url = WebConficData.SmsCenterApi+"version=2.0&details=true";

andafter that I can use url to make some request.之后我可以使用 url 提出一些要求。 For multi-environments web.config and appsettings is avesome.对于多环境 web.config 和 appsettings 非常糟糕。 You just change base url for each env and that's it.您只需为每个环境更改基本 url 即可。

I think the answer to your questions我想你的问题的答案

where do you store the rest client url?您在哪里存储 rest 客户端 url?

is it correct to move the rest url to the config file?将 rest url 移动到配置文件是否正确?

is dependent on how you implement the rest request to that url.取决于您如何实现对 url 的 rest 请求。 As you do not show any information on how you implement the rest call, I would like to show you one possible way and hopefully give you an impression about which things you should consider when implementing a solution.由于您没有显示有关如何实现 rest 调用的任何信息,因此我想向您展示一种可能的方法,并希望让您对在实现解决方案时应该考虑哪些事项有一个印象。

So we can basically (for the sake of completeness) split an rest-endpoint url into two parts which might affect our implementation.所以我们基本上可以(为了完整起见)将一个休息端点 url 分成两部分,这可能会影响我们的实现。

The base url:底座 url:

"https://www.some-nice-name.com/SomeEndPoint"

and the parameters和参数

?key1=value1&key2=value2

having this in mind, you could go the way and split them up, storing the base url and the parameters in two different nodes/attributes in a config file:考虑到这一点,您可以 go 方式并将它们拆分,将基础 url 和参数存储在配置文件中的两个不同节点/属性中:

{
    "BaseUrl" : "https://www.some-nice-name.com/SomeEndPoint",
    "UrlParams" : "?key1=value1&key2=value2"
}

Or in one node/attribute, or even split each single parameter pair ("key1=value1") into own fields.或者在一个节点/属性中,甚至将每个单个参数对(“key1=value1”)拆分为自己的字段。 And so on, and so on......等等等等......

Anyway, if we now jump into our C# code and implement the Rest call, we have a wide range of different possible solution.无论如何,如果我们现在跳入我们的 C# 代码并实现 Rest 调用,我们就有各种不同的可能解决方案。 For this example I will show you how to use the RestSharp NuGet package and why it might influences our decision on the above question.对于这个例子,我将向您展示如何使用 RestSharp NuGet package 以及它为什么会影响我们对上述问题的决定。

So one basic example:所以一个基本的例子:

// I will not show the implementation of ReadYourConfigStuff() because its fairly clear what should happen here
var config = ReadYourConfigStuff(); 

// Give the BaseUrl to our REST client
var restClient = new RestClient(config.BaseUrl);

// Just taking GET as we have some GET Parameters
var restRequest = new RestRequest(Method.GET); 

so far so good.到目前为止,一切都很好。 But we still miss our parameters right?但是我们仍然错过了我们的参数,对吧? Let's go ahead:让我们 go 前进:

// RestSharp gives us a very nice tool to add GET parameters to our request
restRequest.AddParameter("key1", "value1");
restRequest.AddParameter("key2", "value2");

That looks quite different to what we added to our config file, does it?这看起来与我们添加到配置文件中的内容完全不同,是吗? Yes it does.是的,它确实。 As RestSharp gives us a tool at hand which allows to add parameters one by one, we are free to choose how to store and maintain those in our code.由于 RestSharp 为我们提供了一个允许一个一个添加参数的工具,我们可以自由选择如何在我们的代码中存储和维护这些参数。 And if we have a look on the AddParameter defintion如果我们看看 AddParameter 定义

public IRestRequest AddParameter(string name, object value);

we see that the second parameter can be any object.我们看到第二个参数可以是任何 object。 Amazing!惊人!

So my answer to your question is: Yes you can store it in the config file, but does it fit to your implementation?所以我对你的问题的回答是:是的,你可以将它存储在配置文件中,但它适合你的实现吗? Are the parameters fix or do they change?参数是固定的还是改变了? How does your favorite tooling would like you to implement the rest request?您最喜欢的工具希望您如何实现 rest 请求?

Based on the answers to these questions, I would take a decision rather to use a config file or not.根据这些问题的答案,我会决定是否使用配置文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM