英文:
How to redirect a URL to another URL in C#
问题
我有一个ASP.NET Core MVC网页应用程序,我需要将特定的链接重定向到另一个链接。例如,我想将所有来自http://example.com
的请求重定向到http://RedirectLink.com
。
我该如何在我的ASP.NET Core MVC应用程序中使用C#来实现这一点?
我尝试使用以下代码:
public IActionResult RedirectExample()
{
// 获取请求的URL
string requestedUrl = HttpContext.Request.GetEncodedUrl();
// 检查请求的URL是否与特定的URL匹配
if (requestedUrl.Equals("https://ExampleLink.com"))
{
// 重定向到新的URL
return Redirect("https://RedirectLink.com");
}
// 如果请求的URL与特定的URL不匹配
// 只需返回一个普通视图
return View();
}
我期望这段代码能够检查requestedURL
是否被访问,然后执行重定向到https://RedirectLink.com
。
英文:
I have an ASP.NET Core MVC web application and I need to redirect a specific link to another link. For example, I want to redirect all requests from http://example.com
to http://RedirectLink.com
.
How can I achieve this using C# in my ASP.NET Core MVC application?
I tried to use the following code.
public IActionResult RedirectExample()
{
// Get the requested URL
string requestedUrl = HttpContext.Request.GetEncodedUrl();
// Check if the requested URL matches the specific URL
if (requestedUrl.Equals("https://ExampleLink.com))
{
// Redirect to the new URL
return Redirect("https://RedirectLink.com");
}
// If the requested URL doesn't match the specific URL
// just return a normal view
return View();
}
I was expecting this to be able to see if the requestedURL
gets hit then it will then perform a redirect to https://RedirectLink.com
.
答案1
得分: 2
我想将所有来自http://example.com的请求重定向到http://RedirectLink.com。我如何在我的MVC应用程序中使用C#实现这一点?
实际上,有多种方法可以实现这一点。您可以在控制器级别或中间件中执行,甚至可以使用IIS HTTP重定向模块。
如何实现:
假设我有一个名为example.com的域,因此该域可能包含许多路径,例如example.com/home/index或其他内容。因此,无论用户是否访问基本的example.com或example.com/home/index,您都可以将其重定向到RedirectLink.com。
现在,让我们看看如何在实际操作中实现这一点:
public class RedirectionMiddleware
{
private readonly RequestDelegate _next;
public RedirectionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var originalUrl = context.Request.GetDisplayUrl(); // 检查要验证的URL
if (originalUrl.Contains("http://localhost:5094/AccessTempData/Index"))
{
var redirectToAnotherURL = "https://localhost:44361/userlog/ViewCalculateAge";// 您要重定向的URL
context.Response.Redirect(redirectToAnotherURL);
}
await _next(context);
}
}
注意:您还可以使用context.HttpContext.Request.Path.StartsWithSegments来验证您的条件。
Program.cs:
app.UseMiddleware<RedirectionMiddleware>();
注意:请根据中间件顺序进行操作。
输出:
注意:如果您想使用IIS重定向模块来实现或重定向,您可以在此处查看我们的官方文档。
英文:
> I want to redirect all requests from http://example.com to
> http://RedirectLink.com. How can I achieve this using C# in my MVC
> application?
Actually, using numerous way it can be achieved. You either can do in controller level or in middleware or even using IIS HTTP redirect module.
How to achieve:
Let's assume, I have a domain called example.com so this domain may contains many path for instance, example.com/home/index or anything. Therefore, I would want either user hit base example.com or example.com/home/index now, you would redirect to RedirectLink.com while either of the URL accessed.
Now, have a look how can we can implement that in action:
<!-- language: c# -->
public class RedirectionMiddleware
{
private readonly RequestDelegate _next;
public RedirectionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var originalUrl = context.Request.GetDisplayUrl(); // Check the URL which you want to validate
if (originalUrl.Contains("http://localhost:5094/AccessTempData/Index"))
{
var redirectToAnotherURL = "https://localhost:44361/userlog/ViewCalculateAge";//URL you want to redirect
context.Response.Redirect(redirectToAnotherURL);
}
await _next(context);
}
}
Note: You can also check context.HttpContext.Request.Path.StartsWithSegments to validate your condition.
Program.cs:
<!-- language: c# -->
app.UseMiddleware<RedirectionMiddleware>();
Note: Please beaware of middleware order accordingly.
Output:
Note: If you would like to implement or redirect using IIS Redirect module you could check our official document here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论