我正在努力解决这个问题HttpResponse.Redirect.我以为它会包括在内System.Web但我得到了
"响应"名称在当前上下文中不存在"错误.
这是整个控制器:
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
namespace MvcApplication1.Controllers
{
public class SmileyController : ApiController
{
public HttpResponseMessage Get(string id)
{
Response.Redirect("http://www.google.com");
return new HttpResponseMessage
{
Content = new StringContent("[]", new UTF8Encoding(), "application/json"),
StatusCode = HttpStatusCode.NotFound,
};
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图抓住InvalidOperationException在声明变量时有时会发生的事情.但是,以下代码不起作用.可能是因为我真的不知道你是如何捕获异常的.
public override void Download()
{
try
{
var t = (ForumThread)Globals.Db.Thread.Get(_extIdForumThread, _idF);
try
{
throw new InvalidOperationException();
}
catch (InvalidOperationException exception)
{
return;
}
catch (Exception exception)
{
throw;
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都将非常感激.
我只是想在我的webrequest中设置一个cookie但由于错误"无法将类型'void'隐式转换为'System.Net.CookieContainer'"而无法执行此操作.
如果下面的代码不起作用 - 你怎么做到这一点?
public static string GET(string url, string cookieValue, string cookieName)
{
string response = "";
Uri target = new Uri(url);
var host = target.Host;
Cookie c = new Cookie(cookieValue, cookieName) { Domain = host };
CookieContainer cookieContainer = new CookieContainer().Add(c);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "GET";
request.CookieContainer = cookieContainer;
WebResponse response1 = request.GetResponse();
Stream dataStream = response1.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
response = reader.ReadToEnd();
reader.Close();
response1.Close();
return response;
}
Run Code Online (Sandbox Code Playgroud)