如何在 ASP.NET Core MVC 中获取 Url Referrer? [英] How can I get Url Referrer in ASP.NET Core MVC?

查看:61
本文介绍了如何在 ASP.NET Core MVC 中获取 Url Referrer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ASP.NET MVC 网络表单迁移到 ASP.NET Core MVC.目前,我在使用 Request.UrlReferrer 类时遇到问题.

I am trying to migrate an ASP.NET MVC webform to ASP.NET Core MVC. Currently, I am having trouble with the Request.UrlReferrer class.

原文是:

    [HttpPost]
    public async Task<ActionResult> ContactUsFormSubmit(ContactUs request)
    {
        var siteUrl = Request.UrlReferrer.ToString().ToLower();
        ....
    }

但是,对于 ASP.NET Core,UrlReferrer 不可用.我发现了以下内容:

However, with ASP.NET Core, UrlReferrer is not available. I have found the following:

    Request.Headers["Referer"]

返回 StringValues 而不是 String.我不确定我是否应该尝试使用这个,或者是否有任何其他解决方案可以解决这种情况.Request.ServerVariables 也不可用,或者我可能没有命名空间.我的命名空间如下:

which returns StringValues instead of a String. I am not sure if I should try to use this one or if there is any other solutions to this situation. Request.ServerVariables is also not available or maybe I don't have the namespace. My namespaces are as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

如果有人能指导我朝着正确的方向前进,我将不胜感激.

I would really appreciate if someone can direct me in the right direction.

推荐答案

大功告成.StringValues 类只是 ASP.NET 用来在框架中有效表示字符串的一种类型.特别是在 HttpContext 对象中.您可以调用 ToString() 将其转换为字符串:

You're almost there. The StringValues class is just a type ASP.NET uses to efficiently represent strings in the framework. Especially in the HttpContext object. You can just call ToString() on it to convert it to a string:

string referer = Request.Headers["Referer"].ToString();

这篇关于如何在 ASP.NET Core MVC 中获取 Url Referrer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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