C# MVC 4 RedirectToAction未看到自定义标头
如果您启动了一个新的Web项目,并创建了一个新的MVC4应用程序(子类为“WebApi”),您可以将下面的代码粘贴到(overwriting HomeController.cs)中以使代码正常工作 我有一个MVC4应用程序(带有WebApi) 我正在尝试在MVC控制器方法中设置自定义标头,然后执行重定向操作。在第二个MVC控制器方法中看不到自定义标头 我能够在第一个mvc控制器方法中设置cookie,并在第二个mvc控制器方法中看到它(在重定向操作之后) 有没有办法查看我在重定向操作后在第二个mvc控制器方法中设置的自定义头 谢谢C# MVC 4 RedirectToAction未看到自定义标头,c#,asp.net,asp.net-mvc,asp.net-mvc-4,asp.net-web-api,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Asp.net Web Api,如果您启动了一个新的Web项目,并创建了一个新的MVC4应用程序(子类为“WebApi”),您可以将下面的代码粘贴到(overwriting HomeController.cs)中以使代码正常工作 我有一个MVC4应用程序(带有WebApi) 我正在尝试在MVC控制器方法中设置自定义标头,然后执行重定向操作。在第二个MVC控制器方法中看不到自定义标头 我能够在第一个mvc控制器方法中设置cookie,并在第二个mvc控制器方法中看到它(在重定向操作之后) 有没有办法查看我在重定向操作后在第二个m
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace MyMvc4WebApiProjectNamespace.Controllers
{
public class HomeController : Controller
{
private const string CustomCookieName = "CustomCookieName";
private const string CustomHeaderName = "X-CustomHeaderName";
private const string IISExpressRootUrl = "http://localhost:55937/"; /* open up the project properties and go to the web tab and find the iis-express area to get the correct value for your environment */
public ActionResult Index()
{
IEnumerable<string> webApiValues = null;
string value1 = null;
string value2 = null;
HttpClientHandler handler = new HttpClientHandler
{
UseDefaultCredentials = true,
PreAuthenticate = true
};
using (var client = new HttpClient(handler))
{
string valuesUri = IISExpressRootUrl + "api/Values";
webApiValues = client
.GetAsync(valuesUri)
.Result
.Content.ReadAsAsync<IEnumerable<string>>().Result;
if (null != webApiValues)
{
value1 = webApiValues.ElementAt(0);
value2 = webApiValues.ElementAt(1);
}
else
{
throw new ArgumentOutOfRangeException("WebApi call failed");
}
}
HttpCookie customCookie = new HttpCookie(CustomCookieName, "CustomCookieValue_ThisShowsUpIn_MyHomeControllerAlternateActionResult_Method");
Response.Cookies.Add(customCookie);
HttpContext.Response.AppendHeader(CustomHeaderName, "CustomHeaderValue_This_Does_Not_Show_Up_In_MyHomeControllerAlternateActionResult_Method");
//Response.AppendHeader(CustomHeaderName, value2);
return RedirectToAction("MyHomeControllerAlternateActionResult");
}
public ActionResult MyHomeControllerAlternateActionResult()
{
IEnumerable<string> webApiReturnValues = null;
CookieContainer cookieContainer = new CookieContainer();
foreach (string cookiename in Request.Cookies)
{
if (cookiename.Equals(CustomCookieName, StringComparison.OrdinalIgnoreCase))
{
var cookie = Request.Cookies[cookiename];
cookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, "localhost"));
}
}
if (cookieContainer.Count < 1)
{
throw new ArgumentOutOfRangeException("CookieContainer did not find the cookie I was looking for");
}
else
{
Console.WriteLine("This is what actually happens. It finds the cookie.");
}
HttpClientHandler handler = new HttpClientHandler
{
UseCookies = true,
UseDefaultCredentials = true,
PreAuthenticate = true,
CookieContainer = cookieContainer
};
using (var client = new HttpClient(handler))
{
bool customHeaderWasFound = false;
if (null != this.Request.Headers)
{
if (null != this.Request.Headers[CustomHeaderName])
{
IEnumerable<string> headerValues = this.Request.Headers.GetValues(CustomHeaderName);
client.DefaultRequestHeaders.Add(CustomHeaderName, headerValues);
customHeaderWasFound = true;
}
}
/*I wouldn't expect it to be in the below, but I looked for it just in case */
if (null != this.Response.Headers)//
{
if (null != this.Response.Headers[CustomHeaderName])
{
IEnumerable<string> headerValues = this.Response.Headers.GetValues(CustomHeaderName);
client.DefaultRequestHeaders.Add(CustomHeaderName, headerValues);
customHeaderWasFound = true;
}
}
if (!customHeaderWasFound)
{
Console.WriteLine("This is what actually happens. No custom-header found. :( ");
}
string valuesUri = IISExpressRootUrl + "api/Values";
webApiReturnValues = client
.GetAsync(valuesUri)
.Result
.Content.ReadAsAsync<IEnumerable<string>>().Result;
if (null == webApiReturnValues)
{
throw new ArgumentOutOfRangeException("WebApi call failed");
}
}
return View(); /* this will throw a "The view 'MyHomeControllerAlternateActionResult' or its master was not found or no view engine supports the searched locations" error, but that's not the point of this demo. */
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用System.Web;
使用System.Web.Mvc;
使用System.Web.Security;
命名空间MyMvc4WebApiProjectNamespace.Controllers
{
公共类HomeController:控制器
{
私有常量字符串CustomCookieName=“CustomCookieName”;
私有常量字符串CustomHeaderName=“X-CustomHeaderName”;
私有常量字符串IISExpressRootUrl=”http://localhost:55937/“;/*打开项目属性并转到“web”选项卡,找到“iis express”区域,以获取适合您环境的正确值*/
公共行动结果索引()
{
IEnumerable webApiValues=null;
字符串值1=null;
字符串值2=null;
HttpClientHandler处理程序=新的HttpClientHandler
{
UseDefaultCredentials=true,
预验证=真
};
使用(var客户端=新的HttpClient(处理程序))
{
字符串值suri=IISExpressRootUrl+“api/Values”;
webApiValues=客户端
.GetAsync(valuesUri)
.结果
.Content.ReadAsAsync().Result;
if(null!=webApiValues)
{
value1=webApiValues.ElementAt(0);
value2=webApiValues.ElementAt(1);
}
其他的
{
抛出新ArgumentOutOfRangeException(“WebApi调用失败”);
}
}
HttpCookie customCookie=新的HttpCookie(CustomCookieName,“CustomCookieValue\u ThisShowsUpIn\u MyHomeControllerAlternateActionResult\u方法”);
Response.Cookies.Add(customCookie);
HttpContext.Response.AppendHeader(CustomHeaderName,“CustomHeaderValue\u此\u未\u在\u MyHomeControllerAlternateActionResult\u方法中显示”);
//响应.附录标题(CustomHeaderName,value2);
返回重定向到操作(“MyHomeControllerAlternateActionResult”);
}
公共操作结果MyHomeControlleraterActionResult()
{
IEnumerable webApiReturnValues=null;
CookieContainer CookieContainer=新CookieContainer();
foreach(Request.Cookies中的字符串cookiename)
{
if(cookiename.Equals(CustomCookieName、StringComparison.OrdinalIgnoreCase))
{
var cookie=Request.Cookies[cookiename];
添加(新Cookie(Cookie.Name,Cookie.Value,Cookie.Path,“localhost”);
}
}
如果(cookieContainer.Count<1)
{
抛出新ArgumentOutOfRangeException(“CookieContainer没有找到我要找的cookie”);
}
其他的
{
WriteLine(“这是实际发生的事情,它找到了cookie。”);
}
HttpClientHandler处理程序=新的HttpClientHandler
{
UseCookies=true,
UseDefaultCredentials=true,
预验证=真,
CookieContainer=CookieContainer
};
使用(var客户端=新的HttpClient(处理程序))
{
bool customHeaderWasFound=false;
if(null!=this.Request.Headers)
{
if(null!=this.Request.Headers[CustomHeaderName])
{
IEnumerable headerValues=this.Request.Headers.GetValues(CustomHeaderName);
client.DefaultRequestHeaders.Add(CustomHeaderName、headerValue);
customHeaderWasFound=true;
}
}
/*我没想到它会在下面,但我找了它以防万一*/
if(null!=this.Response.Headers)//
{
if(null!=this.Response.Headers[CustomHeaderName])
{
IEnumerable headerValues=this.Response.Headers.GetValues(CustomHeaderName);
client.DefaultRequestHeaders.Add(CustomHeaderName、headerValue);
customHeaderWasFound=true;
}
}
如果(!customHeaderWasFound)
{
WriteLine(“这是实际发生的情况。未找到自定义标题:”;
}
字符串值suri=IISExpressRootUrl+“api/Values”;
webapireutvalues=client
.GetAsync(valuesUri)
.结果
.Content.ReadAsAsync().Result;
if(null==webapirurnvalues)
{
抛出新ArgumentOutOfRangeException(“WebApi调用失败”);
}
}
return View();/*这将抛出一个“视图‘MyHomeControllerateActionResult’或其主视图未找到或没有视图引擎支持搜索的位置”错误,但这不是重点