从基地+相对URL在C#中的绝对URL [英] Absolute URL from base + relative URL in C#

查看:172
本文介绍了从基地+相对URL在C#中的绝对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的URL:

  http://my.server.com/folder/directory/sample 

和一个相对的:

  ../../其他/路径

如何获得绝对URL由此 ?这是非常简单明了使用字符串操作,但我想这样做安全的方式,使用乌里类或类似的东西。



这是一个标准的一个C#应用程序,而不是一个ASP.NET的。


解决方案

  VAR基本URI =新的URI(http://my.server.com/folder/directory/sample); 
变种绝对URI =新的URI(基本URI,../../等/路径);

 开放的URI; 
如果(Uri.TryCreate(HTTP://基/,../relative,出URI))的doSomething(URI);


I have a base URL :

http://my.server.com/folder/directory/sample

And a relative one :

../../other/path

How to get the absolute URL from this ? It's pretty straighforward using string manipulation, but I would like to do this in a secure way, using the Uri class or something similar.

It's for a standard a C# app, not an ASP.NET one.

解决方案

var baseUri = new Uri("http://my.server.com/folder/directory/sample");
var absoluteUri = new Uri(baseUri,"../../other/path");

OR

Uri uri;
if ( Uri.TryCreate("http://base/","../relative", out uri) ) doSomething(uri);

这篇关于从基地+相对URL在C#中的绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆