RedirectToAction in same controller not working? - c#

So I'm trying to use RedirectToAction() to redirect to another action in the same controller. However, everything that I have tried has not worked. Ideally I want to be able to type the link /projects/assign/2 and be redirected to the /projects/create page.
Examples of what I have tried:
[HttpGet]
public IActionResult Assign(int? id)
{
return View("Create", id);
}
And:
[HttpGet]
public IActionResult Assign(int? id)
{
return View("Create");
}
When I type /projects/assign/2 in the address bar, I receive an error that says:
"This page isn't working. If the problem continues, contact the site
owner."
At this point, I'm not sure what I am doing wrong and how to make it work.
Any help/advice is greatly appreciated! Thank you!

Apply the following Route attributes:
[Route("Create/{id:int?}")]
[Route("Assign/{id:int?}")]
public IActionResult Create(int? id)
{
...
}
For more information see Routing in ASP.NET Core

just fix the action route
[HttpGet("{id?}")]
public IActionResult Assign(int? id)
{
return View("Create", id);
}

Related

How to solve issue related to ActionName in Asp.Net MVC?

I am working on Asp.Net MVC application where I don't want to display define action name in url. So I am applying ActionName with Action in controllers. But when I am defining ActionName with Action then it's not working and showing error page of 404 - not found. So please can you guide me to resolve this issue ?
public ActionResult Login()
{
//code
}
when I am passing url like domain/Login then it's working properly.
[ActionName("SignIn")]
public ActionResult Login()
{
//Code
}
but when I am applying ActionName like above then it's showing error page of 404.
I check about this issue on google but not any work for me.
While returning from the controller , for the same controller you have to specify like
return View("Login");
If you are redirecting to another controller action
return RedirectToAction("ActionName", "ControllerName");
and if you are using javascript to navigate for this action,use like
window.location.href="/ControllerName/SignIn";
Try
[ActionName("SignIn")]
public ActionResult Login()
{
//Code
return View("Login");
}

I can't to call action method in the controller from view

I want to call the "Delete Person" method from the "Home" controller on the link provided by the "Index" view, but get the error: "could not find this resource": HTTP 404, URL: /Home/DeletePerson/1. I tried #Html.ActionLink, but it doesn't work either. Where is my mistake?
The project has .NET Framework 4.7.2, Entity Framework 6.2.0, MVC 5.
This project has HomeController:
public class HomeController : Controller
{
...
public ViewResult Index()
{
...
return View("Index");
}
[HttpGet]
public ActionResult DelelePerson(int id)
{
...
return View(person);
}
[HttpPost]
public ActionResult DeletePersonConfirmed(int id)
{
...
return RedirectToAction("Index");
}
}
My Index.cshtml includes
<td><p>Del</p></td>
First, use Html.ActionLink to create the correct link They are aware of your specific routing configuration. Second, your link should probably look like "/Home/DeletePerson?id=#b.id", but that depends on your routing configuration which you haven't posted.

How to RedirectToAction with a model passed?

I have an model class that is used to validate some user input.
I have an controller with the following.
public IActionResult Checkout(GiftCard giftCard)
{
}
I was wondering how I could on an different action redirect it back to it such as
public IActionResult Preview(GiftCard giftCard)
{
return RedirectToAction("Checkout");
}
The above doesn't work because asp.net is trying to find an action without the model like the one below
public IActionResult Checkout()
{
}
if your url going to be really long make a shorturl so load that and redirect from there, if you use ajax it wont be visible
You could use action with another name and apply action selector to your renamed method. Like next:
[ActionName("Checkout")]
[HttpPost] //Recomend you send user input via post
[ValidateAntiForgeryToken] // and use validation token
public IActionResult CheckoutConfirmed(GiftCard giftCard)
{
//your code
}
public IActionResult Checkout()
{
//your code
}
Check out more about ASP.NET MVC - Selectors
If you need more information about ValidateAntiForgeryToken, you could find it there - Chapter 12: Security
And also, you could find great article about posting there - ASP.NET MVC Preview 5 and Form Posting Scenarios
RedirectToAction has a second parameter called routeValues with which you can pass the GiftCard like following.
public IActionResult Preview(GiftCard giftCard)
{
return RedirectToAction("Checkout", giftCard);
}

ASP.net MVC Routing problems

I have two problems;
First, I can not go to the main page URL. It is not working as it is on the login page:
public ActionResult Index()
{
return View();
}
public ActionResult Login(string email, string password)
{
model = BLcontext.GetUserLogin(email, password);
if (model.UserID > 0)
{
return Redirect("/Home/Index");
or return PartialView("Index");
or return RedirectToAction("Index");
}
return PartialView("Login");
}
Second, how can I get userId value?
clientAuth "c495600a-71b3-44cb-a577-634426597c82,{\"UserID\":2,\"CurrentSessionID\":\"00000000-0000-0000-0000-000000000000\"},16.01.2015 15:50:53"
I did it
clientAuth.Split(',')[1]
but I see it.. I want value 2
return :"{\"UserID\":2"
I'm not entirely sure what or where your problem is based on what you've submitted. However, it should be as easy as this:
Home Controller: (Default Defined in your Routes)
public ActionResult Index()
{
return View();
}
By default that is a Get, which will automatically be returned when you call the raw controller.
On your other page, when you have an Event that occurs from a user interaction, you simply declare the method and direct to proper action like:
[HttpPost]
public ActionResult ValidateCustomer(int? id)
{
if(id != null)
return View("/SomeOtherController/Example");
return View("/Home/Index");
}
That would take the <form action="/Home/ValidateCustomer" method="POST"> and automatically force the submit input to pass your parameter in your fields, then go to the ActionResult ValidateCustomer hopefully this clarifies navigation a bit for you.

GET and POST methods with the same Action name in the same Controller [duplicate]

This question already has answers here:
MVC [HttpPost/HttpGet] for Action
(4 answers)
Closed 2 years ago.
Why is this incorrect?
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
Some Code--Some Code---Some Code
return View();
}
[HttpPost]
public ActionResult Index()
{
Some Code--Some Code---Some Code
return View();
}
}
How can I have a controlller thas answer one thing when is "getted" and one when is "posted"?
Since you cannot have two methods with the same name and signature you have to use the ActionName attribute:
[HttpGet]
public ActionResult Index()
{
// your code
return View();
}
[HttpPost]
[ActionName("Index")]
public ActionResult IndexPost()
{
// your code
return View();
}
Also see "How a Method Becomes An Action"
While ASP.NET MVC will allow you to have two actions with the same name, .NET won't allow you to have two methods with the same signature - i.e. the same name and parameters.
You will need to name the methods differently use the ActionName attribute to tell ASP.NET MVC that they're actually the same action.
That said, if you're talking about a GET and a POST, this problem will likely go away, as the POST action will take more parameters than the GET and therefore be distinguishable.
So, you need either:
[HttpGet]
public ActionResult ActionName() {...}
[HttpPost, ActionName("ActionName")]
public ActionResult ActionNamePost() {...}
Or,
[HttpGet]
public ActionResult ActionName() {...}
[HttpPost]
public ActionResult ActionName(string aParameter) {...}
I like to accept a form post for my POST actions, even if I don't need it. For me it just feels like the right thing to do as you're supposedly posting something.
public class HomeController : Controller
{
public ActionResult Index()
{
//Code...
return View();
}
[HttpPost]
public ActionResult Index(FormCollection form)
{
//Code...
return View();
}
}
To answer your specific question, you cannot have two methods with the same name and the same arguments in a single class; using the HttpGet and HttpPost attributes doesn't distinguish the methods.
To address this, I'd typically include the view model for the form you're posting:
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
Some Code--Some Code---Some Code
return View();
}
[HttpPost]
public ActionResult Index(formViewModel model)
{
do work on model --
return View();
}
}
You received the good answer to this question, but I want to add my two cents. You could use one method and process requests according to request type:
public ActionResult Index()
{
if("GET"==this.HttpContext.Request.RequestType)
{
Some Code--Some Code---Some Code for GET
}
else if("POST"==this.HttpContext.Request.RequestType)
{
Some Code--Some Code---Some Code for POST
}
else
{
//exception
}
return View();
}
Can not multi action same name and same parameter
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(int id)
{
return View();
}
althought int id is not used
You can't have multiple actions with the same name. You could add a parameter to one method and that would be valid. For example:
public ActionResult Index(int i)
{
Some Code--Some Code---Some Code
return View();
}
There are a few ways to do to have actions that differ only by request verb. My favorite and, I think, the easiest to implement is to use the AttributeRouting package. Once installed simply add an attribute to your method as follows:
[GET("Resources")]
public ActionResult Index()
{
return View();
}
[POST("Resources")]
public ActionResult Create()
{
return RedirectToAction("Index");
}
In the above example the methods have different names but the action name in both cases is "Resources". The only difference is the request verb.
The package can be installed using NuGet like this:
PM> Install-Package AttributeRouting
If you don't want the dependency on the AttributeRouting packages you could do this by writing a custom action selector attribute.
Today I was checking some resources about the same question and I got an example very interesting.
It is possible to call the same method by GET and POST protocol, but you need to overload the parameters like that:
#using (Ajax.BeginForm("Index", "MyController", ajaxOptions, new { #id = "form-consulta" }))
{
//code
}
The action:
[ActionName("Index")]
public async Task<ActionResult> IndexAsync(MyModel model)
{
//code
}
By default a method without explicit protocol is GET, but in that case there is a declared parameter which allows the method works like a POST.
When GET is executed the parameter does not matter, but when POST is executed the parameter is required on your request.

Categories