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

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");
}

Related

RedirectToAction in same controller not working?

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);
}

RedirectToRoute doesn't go to controller

I am using RedirectToRoute in my API controller to redirect to MVC controller. I was unable to find a good solution other than this:
return RedirectToRoute("Cv", new { action = "Cv/OpenCv" });
Previously I tried this below, but it didn't work.
return RedirectToRoute("Cv", new { controller="Cv", action = "OpenCv" });
Api and MVC Controllers use different Routes. RedirectToRoute in Api controller returns "System.Web.Http.Results.RedirectToRouteResult" but RedirectToRoute in Mvc Controller returns "System.Web.Mvc.RedirectToRouteResult". I think you can use just "Redirect" method in this case.
You can try to create an Mvc UrlHelper instance and use a RouteUrl method to build an Url from route parameters and then call Redirect method.
You can redirect from API controller to MVC controller by the help of below code.
Please find below code for the above question.
public IHttpActionResult Index()
{
var newUrl = this.Url.Link("Default", new
{
Controller = "Account",
Action = "Register"
});
return Redirect(new Uri(newUrl.ToString(), UriKind.RelativeOrAbsolute));
}
or follow the direct call from API Controller
public IHttpActionResult Index()
{
return Redirect(new Uri("/Account/Register", UriKind.RelativeOrAbsolute));
}
Please use the above code and let me know

How to RedirectToAction in asp.net-core when I use [HttpGet("public-profile/{avatarName}")]

My problem is that I've added a new route [HttpGet("public-profile/{avatarName}")] in PublicProfile action and I don't know how to redirect to this Action.
So this is the Action that I want get redirected
[HttpGet("public-profile/{avatarName}")]
public async Task<ActionResult> PublicProfile(string avatarName)
{
return View();
}
What I had try was this way
[HttpPost]
public ActionResult PublicProfle()
{
return RedirectToAction("PublicProfile", "name");
}
However it isn't working
I've read about it here but it doesn't help me
RedirectToAction As Get Request format
RedirectToAction with parameter
ASP.Net Core MVC RedirectToAction is appending controller name in front of returnUrl
Try
return RedirectToAction("PublicProfile", new { avatarName = "name" });

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);
}

Razor form not passing back to controller method

I am trying to pass a textbox's text back to a method in my controller, but the data is not being passed in the parameter
I am genuinely confused, i'm following another example but i'm getting a different result, ie - my method behaving as if no parameter is passed
Code
public ActionResult Index(string searchString)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
var listOfAnimals = db.Animals.ToList();
if (!String.IsNullOrEmpty(searchString))
{
listOfAnimals = listOfAnimals.Where(a => a.AnimalName.ToLower().Contains(searchString.ToLower())).ToList();
}
return View(listOfAnimals);
}
and here is my razor form from my view page
#using(Html.BeginForm("Index", "Home"))
{
#Html.TextBox("searchString")
<input type="submit" id="Index" value="Index" />
}
Can anybody spot why this isn't working?
If more code is needed, please let me know but i think the issue is isolated to here
You code is correct.
Since you didn't add [HttpGet] or [HttpPost] before index method.
This method was called twice.
The first call ran when producing the page with form via url http://server/Home/Index. This call was an http get and searchString mapped from URL was null, which is correct.
The second call ran when you clicked submit button. Correct value would be mapped by MVC correctly.
You need to have 2 Index actions (two methods), one without decorations (GET verb) and another one decorated with HttpPost (POST verb). Basically, when you go to the index page, the GET action is executed. When you submit the form, a POST request is executed and the Index decorated with HttpPost is executed.
// GET
public ActionResult Index() { ... }
// POST
[HttpPost]
public ActionResult Index(string searchString) { ... }
Francisco Goldenstein wrote the recommended way. It means you can have two Index() actions:
// for GET
public ActionResult Index() { ... }
// for POST
[HttpPost]
public ActionResult Index(string searchString) { ... }
However it is possible to have one Index() method for handling both (GET and POST) requests:
public ActionResult Index(string searchString = "")
{
if(!string.IsNullOrEmpty(searchString)
{ /* apply filter rule here */ }
}
You wrote, your code is not working. Do you mean, your action method is not requested after click on the button? Consider to allow empty value Index(string searchString = "")
If your action method is fired but variable is empty, check the name on the View() side. Textbox must not be disabled, of course.

Categories