如何在ASP.NET Core MVC中获得“网址引荐来源网址"? [英] How can I get Url Referrer in ASP.NET Core MVC?

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

问题描述

我正在尝试将ASP.NET MVC Web表单迁移到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中获得“网址引荐来源网址"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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