I have a controller like bellow
public class MenuController : Controller
{
//
// GET: /Menu/
public ActionResult Index()
{
return View();
}
public RedirectResult logout()
{
return RedirectPermanent("http://www.google.com");
}
}
and I set a break-point on logout for first time if I hit URL in address-bar localhost:(port number)/menu/logout code stop at breakpoint as expected but after that each and time code not stop breakpoint.
I spend around 2-3 hours and found some stack overflow link where some body write clear browser cache I do that and its working but only once ,that means I have to clear cache each time before debug .I use fire-fox(50.1.0) .try above code to replicate .please help I am stuck badly .
That's what RedirectPermanent() does. It basically tells the browser, "This resource will never be working again, so always request this redirected URL instead." So the browser remembers that and doesn't bother requesting a resource that it's been told will never work again.
If you want the redirect to be temporary, don't make it permanent:
return Redirect("http://www.google.com");
Because you're calling RedirectPermanent
Your browser is (correctly) caching the fact that visiting /logout permanently redirects to (in this case) Google.
It's performing a 301 redirect.
Use Redirect instead
public RedirectResult logout()
{
return Redirect("http://www.google.com");
}
Related
In my React Native app, I'm trying to hit an endpoint in an API written in .Net. I've got the API code on my local machine, so I'm trying to hit it with a call to fetch(127.0.0.1:5000/api/token). This returns a 404 error. If I instead hit fetch(127.0.0.1:5000/), it returns successfully. The API code hit in this case is HomeController.cs, which contains this:
namespace InTowAPI2.Controllers
{
[AllowAnonymous]
[Route("/a")]
public class HomeController : Controller
{
// GET api/values
[HttpGet]
public string Get()
{
return "Ok";
}
}
}
When I make the call to fetch(127.0.0.1:5000/), it returns "Ok" and hits the breakpoint I set in HomeController.cs. The other file ValuesController.cs that should be hit when I call fetch(127.0.0.1:5000/api/token) contains this code (plus more after it):
namespace InTowAPI2.Controllers
{
[AllowAnonymous]
[Route("/api/[controller]")]
...
...
When I call fetch(127.0.0.1:5000/api/token), the breakpoint in ValuesController.cs is not hit, and it returns a 404 error.
What I tried to do to troubleshoot this was, since HomeController.cs was working, I replaced [Route("/")] with [Route("/api")], and made a call to fetch(127.0.0.1:5000/api). This also returned a 404 error.
What I Want To Know:
Why is it that hitting the API code containing [Route("/")] with fetch(127.0.0.1:5000/) works, but modifying it to hit [Route("/api")] with fetch(127.0.0.1:5000/api) throws a 404 error?
The issue is the routing.
[Route("/api/[controller]")]
This RouteAttribute definition will route the endpoint to the name of the controller.
The other file ValuesController.cs that should be hit...
Seems like you controller name is ValuesController, which would create an endpoint on /api/values, not /api/token.
To fix this, do one of the following things:
Change the name of the controller from ValuesController to TokenController.
Change the RouteAttribute from [Route("/api/[controller]")] to [Route("/api/token")].
I have a FormController with Index action and SimpleController with CorticonIndex action.
I am redirecting to CorticonIndex from Index action. My problem is, I have put a breakpoint at return RedirectToAction() and CorticonIndex().
So,Only for the first time I can see the execution by F11 but for the second time controller is not going to CorticonIndex().
How the RedirectToAction() will work?
Is it only one time execution or can we execute multiple time??
FormController
[HttpPost]
public ActionResult Index(FormCollection formCollection)
{
return RedirectToAction("CorticonIndex", " SimpleController");
}
SimpleController
public ActionResult CorticonIndex()
{
var viewModel = this.Model.GetViewModel(payLoad);
return View(CorticonResponseModel.viewName, viewModel);
}
How the RedirectToAction() will work?
It sends an HTTP 302 response to the browser of the web site along with a Location header. What the browser does with that is up to the browser. Normally, it will use the Location header to submit another request to your server. But, as this is entirely out of the application's control, there are no guarantees.
But in this case you are initially calling the server using an HTTP POST. If your browser reloads the page using an HTTP GET, it will not hit this same action method. If you have an HTTP GET action method named Index, it will call that one instead.
NOTE: FYI - if you remove the HttpPost attribute, your action method will respond to both GET and POST. But that is probably not the solution to your issue, as it is normal to have a separate action method in each case.
With reference to .net mvc redirect to external url.
So you have your controller setup with the redirect as below:
public ActionResult SiteDetails(short id)
{
return Redirect("localhost:1234/Controller/Action");
}
BUT
- nothing happens when you call the action.
- Expecting a redirect and nothing happens.
- Expecting a redirect to another MVC site and nothing happens.
- Not only that - in debug the string url going into the redirect works when copied into the browser.
Why doesn't this work?
Requires 'http://' within the string.
public ActionResult SiteDetails(short id)
{
return Redirect("http://localhost:1234/Controller/Action");
}
How do I return an HTTP 403 from a WebAPI method? I've tried throwing an HttpResponseException with HttpStatusCode.Forbidden, and I've tried
return request.CreateErrorResponse(HttpStatusCode.Forbidden, pEx);
Neither of which work. Both ALWAYS return an HTTP 200. What am I missing? It has to be something simple but I don't see it.
You might have a problem with your routing configuration. Below is a working sample. Put it in your controller and see if it works. If it doesn't, check your routing with a diagnostic tool (i.e. Cobisi Routing Assistant).
public HttpResponseMessage GetSomeString(int id)
{
// This method is not allowed!
return this.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "This method is not allowed!");
}
I'm confused about the behavior of RedirectResult - in some cases (with https), the redirect doesn't seem to happen, but something more like a transfer.
If a user tries to access an internal page without being logged in, they are directed to the login page. After logging in, they're directed back to my app, with query string parameter
schema://host:port/myApp?returnUrl=Inspections.mvc/Edit/43523
The code in the HomeController that handles this looks for the redirectUrl, and does this:
if (returnUrl != null)
{
return Redirect(returnUrl);
}
In my dev environment and one QA environment, I see that a redirect response goes back to the browser, which makes another request, as expected.
But in production and another QA environment (both of which use https), the last redirect doesn't happen. The browser continues to show the url
schema://host:port/myApp?returnUrl=Inspections.mvc/Edit/43523
and displays the content that would be returned by the page Inspections.mvc/Edit/43523.
I'm perplexed - is this expected behavior when RedirectResult is used? Is https the relevant difference?
EDIT: Checking traffic, I see that in the environments using https there IS a redirect (301- moved permanently), but it is back to exactly the same address:
schema://host:port/myApp?returnUrl=Inspections.mvc/Edit/43523
This additional information leaves the mystery as puzzling as ever.
Looking at the source code of RedirectResult class you can see that it should do either a 302 or 301 depending on the kind of redirect you want:
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (context.IsChildAction)
{
throw new InvalidOperationException(MvcResources.RedirectAction_CannotRedirectInChildAction);
}
string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext);
context.Controller.TempData.Keep();
if (Permanent)
{
context.HttpContext.Response.RedirectPermanent(destinationUrl, endResponse: false);
}
else
{
context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);
}
}
It should be working as expected no matter what schema you are using. Did you look at the actual request/response with a http sniffer such as Fiddler?
Maybe your browser is choosing not to update the URL for some reason and the problem is not in the actual redirect/rewrite.