本文介绍了如何在 C# 中解析 HTTP url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的需求是解析Http Urls并相应地调用函数.在我当前的实现中,我使用嵌套的 if-else 语句,我认为这不是优化的方式.你能推荐一些其他有效的方法吗?
My requirement is to parse Http Urls and call functions accordingly. In my current implementation, I am using nested if-else statement which i think is not an optimized way. Can you suggest some other efficient approch?
网址如下:
server/func1
server/func1/SubFunc1
server/func1/SubFunc2
server/func2/SubFunc1
server/func2/SubFunc2
推荐答案
我认为您可以从 System.Uri 类.为它提供一个 URI,您可以按照多种安排提取碎片.
I think you can get a lot of use out of the System.Uri class. Feed it a URI and you can pull out pieces in a number of arrangements.
一些例子:
Uri myUri = new Uri("http://server:8080/func2/SubFunc2?query=somevalue");
// Get host part (host name or address and port). Returns "server:8080".
string hostpart = myUri.Authority;
// Get path and query string parts. Returns "/func2/SubFunc2?query=somevalue".
string pathpart = myUri.PathAndQuery;
// Get path components. Trailing separators. Returns { "/", "func2/", "sunFunc2" }.
string[] pathsegments = myUri.Segments;
// Get query string. Returns "?query=somevalue".
string querystring = myUri.Query;
这篇关于如何在 C# 中解析 HTTP url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!