asp.net MVC 3 action method - asp.net

I have created a partial view and I am using it in admin controller's Home action method. Inside partial view, I have an Account Controller with action method named Logon. I am trying to access main view's URL like this
if (Url.ToString().ToLower().Contains("/admin"))
return Redirect("/Admin/Index");
but I cant get the required result. How to get it admin/home ?

From the comments above it looks like you want to just access the current url. If so, then you can simply use the Request object. E.g.
Request.Url.AbsoluteUri
I wonder why you need this though... what is it you are trying to do exactly?

Related

Server Error '\' in Application

am working on my first MVC 5 application. Unfortunately I have run into an error and after searching the internet for the past 4 hours, cannot figure how to fix it. Basically I keep getting the following error.
I understand what the error means. But my Profile view is supposed to be under the Manage view. Basically when a user logs in they should see a new link that says view my profile. Its location should be /Account/Profile not /Home/Profile.
Do I need to change this in the application routing or should I remove the reference I have to this view in the HomeController and instead move it to the AccountController?
Thanks in advance for the help.
The controller that generates a view doesn't restrict you to only that controller, your links can point to any controller. So in your view when you render the link to the Profile view you should specify the controller name.
In the following example I'm using the overload of ActionLink that lets me specify the controller (i.e. Account) that contains the action (i.e. Profile).
Html.ActionLink( "View My Profile", "Profile", "Account" )
ASP.NET MVC by default checks first for the respective view in \Views\[ControllerDirectory]\, but after that, if it doesn't find a match, then it looks in \Views\Shared folder.
Having a view in the shared directory you can share views across multiple controllers. You can add your Profile view to the Shared subdirectory and you should be fine. If you still prefer to have the Profile view under Account Controller and want to reference from other controller's action then change your return statement to specify the location of the view like below.
public ActionResult ProfileAction()
{
return View("../Account/Profile");
}

ASP.NET MVC 3 - How to restrict areas in an efficient way?

I've got an ASP.NET MVC 3 site with an admin panel (don't we all? :) - I've used my own solution for a very secured login system.
Now, on each view in the admin controller I need to make checks that the user is logged and has the proper authorization, so each time I run the same verification and authorization methods on each view separately.
How could I make the same checks for all the requests to a certain controller? (I mean, right all the checks only once and in one place)
(I also would like to have an exception, so I could allow user to use the login page inside the admin controller and outside of it)
Thanks!
Use an attribute on the controller. Either the standard AuthorizeAttribute (see this) or write your own.
What you're looking for is action filter attributes. They are basically an attribute you can place on a controller that allows you to intercept calls to every action method within a controller and are therefore perfect for security as you can deny/accept requests: http://msdn.microsoft.com/en-us/library/system.web.mvc.actionfilterattribute.aspx
If you want to limit the entire controller instead of the individual actions you could place the [Authorize] attribute like so:
[Authorize]
public class PageController : Controller
{ ... }

asp.net MVC3 View

I want to open a view that says update or insert successful. I did not create an action because I only want a simply status page. I created a view called Status.cshtml
I get a message saying that the view cannot be found. Any idea what is wrong?
Do I need an action for this to work?
When your process has finished successfully use the following line of code...
return View("Status");
Note: There is no action method with the same name - so navigating to www.yourdomain.com/YourController/Status will not route correctly...
Just a View is not enough. Your request first reaches a controller action through Asp.net mvc routing and only if the correct action is found the relevant view is displayed. Here is a video if you need a quick grab of how asp.net controllers and views work.

Asp.Net MVC Not Duplicate forms when Edit/Add

When we have anything that requires user input (Eg adding a product to a database) the Edit screen looks the same as the add screen. When using MVC .Net how do you handle this? Do you return the same view? Just adjust the model to reflect the change?
Same (partial)view
You create only one but strong typed view (depending on the UI it can of course be a partial view as well). When adding new data return this view from controller action with default model object instance (usually just a new instance without any properties being set), but when you edit, return it with the object instance that you'd like to edit.
Controller part
Regarding controller actions you can have four of them:
Add GET
return View("SomeView", new Customer());
Add POST
Edit GET
return View("SomeView", new CustomerRepository().GetCustomer(id));
Edit POST
Bot GET actions return the same view but with different model as described earlier. POST actions both store submitted data, but return whatever they need to. Probably some RedirectToAction()...
You can use the same view for display and Edit, simply call it from your controller
return View("ViewName")
You could have the form fields in a partial view and have two separate views using the same partial view, one posting to the edit controller action method and the other posting to the add controller action method.
Partial views are used to remove duplicity. You could read an example of this in the Nerd Dinner tutorial.

RenderAction not finding action method in current controller in current area

I'm creating an ASP.NET MVC 2 (RTM) project that uses areas. The Index action of the Home controller of one area needs to use RenderAction to generate a sub-section of the page. The action called is also defined in the same Home controller. So the call should just be:
<% Html.RenderAction("List") %>
However, I get an exception:
A public action method 'List' was not found on controller 'RareBridge.Web.Areas.Events.Controllers.HomeController'.
Note that I'm not in the "Events" area! I'm in a completely different area. If I remove the "Events" home controller, then the exception still occurs but names a different controller (still not the one I want it to call).
I've also tried providing the controller name and area to the RenderAction method, but the same exception occurs. What is going on here?
BTW: I am using Autofac as my IoC container
Probably action you call has filter attribute (i.e. AcceptVerbs) which doesn't match current request. Remove filters from "List" action and try again.
Use the renderaction overload which takes routeValues as parameter and use the area property to redirect to a specific area:
f.i.
<% Html.RenderAction("Edit", module.Value, new { area = "Modules", id = module.Key }); %>

Resources