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

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

问题描述

我有一个基本网址:

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

还有一个亲戚:

../../other/path

如何从中获取绝对网址?使用字符串操作非常简单,但我想以安全的方式执行此操作,使用 Uri 类或类似的东西.

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.

它适用于标准的 C# 应用程序,而不是 ASP.NET 应用程序.

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");

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

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

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