how to redirect one area to another area - asp.net-mvc-4

I have two controllers in my MVC4 Project.
One controller is in default controller name called Login.
another controller is in area .area name called HR.inside controller name called AddNewHire.
inside AddNewHire i written AddNewEmployee method.
in Login controller i am having one method .that name is LoginButton
inside LoginButton method i written
return RedirectToAction("AddNewEmployee","AddNewHire",new {area="HR"});
am getting Error Like
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /AddNewHireController/AddNewEmployee

Try using RedirectToRoute instead of RedirectToAction. What you would have to do is to define a route. Have a look at this MSDN link (Walkthrough: Creating an ASP.NET MVC Areas Application Using Multiple Projects

Related

Why is mandatory the "Route" attribute on methods of a custom-routed controller?

Consider a fresh Asp.Net Core 2.1 MVC Web app created via the Visual Studio 2017 template. Now, consider a custom view (MyView) and also a controller (ActualController) so that the project structure looks similar to this picture:
The MyView shows nothing special, and it's off the point. However, the page should appear when the user enters an URL like http://(domain)/desired/myview or also via a hyperlink in the home page:
<a asp-area="" asp-controller="Desired" asp-action="MyView">MyView</a>
Now let's focus on the controller, which is a class named differently from what the routing expects:
[Route("desired")]
public class ActualController : Controller
{
[Route("MyView")] //without this the method won't be called
public IActionResult MyView()
{
return this.View();
}
}
From what I know, by decorating the controller with a Route attribute tells the URL resolver to match this class. However, the mapping works only if I explicitly add a (redundant) Route attribute on the target method/action. If I remove it, the path won't be found, and the server returns a 404-error.
The question is: why should be mandatory to decorate with Route the method, even the action is implicitly defined by the method name (as usual)?
NOTE: is rather simple for me to rename the controller class, but I'd like to know what are the reasons behind this behavior.
You are overriding the default route of [controller]/[action] with [Route("desired")]. Since you don't define an action parameter on controller level, all other routes have to be done explicitly.
Changing the top route parameter to [Route("desired/[action]")] should solve it and the method name will be used as parameter. You can still override single actions if you want to name them differently by adding the [Route("")] attribute to them.
Also see the docs (Token replacement in route templates) for further description on the route parameters

Umbraco with MVC Controller

I am working on MVC and i started learning Umbraco, I didn't get how to bind the umbraco page with mvc controller get method to show the database values. can anyone suggest any url or video?
Thansk...
What you're looking for is Umbraco route hijacking.
You can read about it here.
https://our.umbraco.org/documentation/reference/routing/custom-controllers
It's easiest to demonstrate with an example : let's say you have a Document Type called 'Home'. You can create a custom locally declared controller in your MVC web project called 'HomeController' and ensure that it inherits from Umbraco.Web.Mvc.RenderMvcController and now all pages that are of document type 'Home' will be routed through your custom controller! Pretty easy right :-) OK so let's see how we can extend this concept. In order for you to run some code in your controller you'll need to override the Index Action.
So, basically, you "simply" need to create a controller named after your document type, so for example, a document type with the name "TextPage" would need a controller called "TextPageController". Now, if you read through the documentation, you'll find that your "TextPageController" will need to inherit from the RenderMvcController. Here's an example how to achieve this.
public class TextPageController : RenderMvcController
{
public ActionResult Index()
{
return View("~/Views/TextPage.cshtml");
}
}
This forum link may help you:
https://our.umbraco.org/forum/developers/razor/38242-Umbraco-MVC4111-Surface-controller-using-an-AJAX-form

Asp.NET MVC 4 routing a specific URL to a certain controller

I want to run a certain Asp.NET MVC 4 Controller when an URL -probably irrelevant with the controller name- is entered. For example, if the user opens the address "localhost:3364/abc/def", I want to run the controller with the name, for example, "SugarController". Is it possible, or do I have to start my URL with the word "Sugar"? I know that URL routing can be done by adding some code to Global.asax file in the project. But I don't exactly know how to manage this one.
Thanks in advance.
This should work
routes.MapRoute("Fixed", "abc/def", new { controller = "Sugar", action = "def"});

Creating a new view results in 404 Page

I am trying to do something very simple and I seem to be missing something. I tried to scour the internet for results but haven't gotten anywhere so I was wondering if someone can please advise on this seemingly easy and straightforward task.
I have a working MVC Application and have created Models, Controllers, Views using the defaults (scaffolding).
Now I want to create a new view for one of my controller actions:
public ActionResult Index()
{
return View(db.Blog.ToList());
}
So I right click on Action Result and click Add View.
This gives me a dialog box where I specify a view name of "Test", I click "Create a Strongly Typed View" check box and select model class of Blog.
For scaffold template, I leave empty (note I have tried index without any good result)
Now I click the Add button.
As expected this creates a new view test.cshtml under Views/Blogs
Now when I begin without debugging and go to url: localhost:12341/Blog/Test
I get the following error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Blog/test
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
There could be a lot of issues with why it doesn't work. It is probably worth your while to look into ASP.NET MVC routing. For now, Mystere Man's solution might be a "quick fix" assuming you have everything else set up to their defaults.
In particular, when you tell your browser to go to localhost:12341/Blog/Test then it will (probably) look for a Controller called BlogController and then perform the Test action. In your case, your action is called Index so you would want to go to localhost:12341/Blog/Index (though you may be able to omit Index since it's the default action). Lastly, since your action is called Index then the View() function will automatically look for Index.cshtml. This is detailed somewhat in the msdn documentation:
http://msdn.microsoft.com/en-us/library/dd492930(v=vs.100).aspx
In particular:
If the ViewName property is empty, the current action name is used in place of the ViewName property.
Your action method is called Index, not Test. If you want the url to be /Test, then you need to name the action method Test (there are other ways to do it, but this is the best way)
If you want to use the view Test, then you need to specify it in your View() method.
return View("Test", db.Blogs.ToList());
However, you will still need to use the Index url /Blogs/Index because your action method is Index.

Yii generate errors from parent controller

I just started using Yii, coming from Codeigniter (CI). I'm trying to set up a situation where the application will check a users credentials before they can even access the site. In CI, I would create a parent controller, put my checks in there, and then inherit that parent controller. I've been trying to do the same thing with Yii, but with no luck.
I first created an init() method (for some reason I can't put the code in a __construct() method). I then perform my check, which works fine. I can throw a new CHttpException, but that looks ugly. How do I use Yii's built in error handling to accomplish what I want to do?
For simple login rules you should just implement the 'accessControl' filters from Yii see Yii documentation on authorizations.
Another way would be to throw the Exception like you already did, and write custom Http error views, place it in the yerfolder/protected/views/system/ (see the yii/framework/views directory for examples)
I solved this by sending the error to the site/error view and then calling Yii::app()->end() to keep the child controllers from loading.