Test That A Controller Action Simply Returns A Link To Another Action?

Apr 29, 2010

Lets say I have a simple controller for ASP.NET MVC I want to test. I want to test that a controller action (Foo, in this case) simply returns a link to another action (Bar, in this case).How would you test TestController.Foo? (either the first or second link)

My implementation has the same link twice. One passes the url throw ViewData[]. This seems more testable to me, as I can check the ViewData collection returned from Foo(). Even this way though, I don't know how to validate the url itself without making dependencies on routing.The controller:

public class TestController : Controller
public ActionResult Foo()[code].....

View 1 Replies

Similar Messages:

Testing Controller Action Which Returns RedirectToRouteResult?

May 19, 2010

I have an action in my controller:

RedirectToRouteResult Create(UserDTO UserDTO)

Which at some point decides with which HTML to respond after a post request by redirecting to an action:

return ModelState.IsValid ? RedirectToAction("ThanksCreate") : RedirectToAction("Register");

In my unit tests I would like to get hold of the 'views' modelstate somehow like this:

var modelState = result.ViewData.ModelState;
Assert.IsFalse( modelState.IsValid );

where 'result' (ViewResult) is the result of the action 'Create' depending on the submitted DTO. My dilemma is that my action 'returns' a RedirectToRouteResult which I thought is quite nice but it might not be testable or is it?

How could I get hold of the ModelState in my scenario?

View 1 Replies

MVC :: Pass ID Value To Home Controller Index Action? (like Www.test.com/23)

Jun 18, 2010

I want to have the following URL pattern in my MVC application:

www.test.com -> display welcome page

www.test.com/5 -> display article with ID 5

I have set up my Home Controller like this:


When I request www.test.com, everything works as expected (the Index action is invoked with the ID parameter set to NULL).

But when I request www.test.com/5 it says: HTTP 404 Resource not found. When I request www.test.com/?id=5 it works.

View 2 Replies

Want To Try And Test An Action Method On Controller That Itself Tests If IsAjaxRequest() Is True?

May 28, 2010

I'm just starting to use the TestHelpers in MvcContrib. I want to try and test an action method on my controller that itself tests if IsAjaxRequest() is true. I've used the same code that is shown in the TestHelper samples to set up the TestControllerBuilder

_controller = new StarsController();
_builder = new TestControllerBuilder();

So that _controller has all the faked/mocked HttpContext inside it, which is really great. But what do I do now to force IsAjaxRequest() on the internally faked Request object to return true?

View 2 Replies

MVC :: How To Get An Action Link From Within The Controller

Dec 21, 2010

I know I can use Html.ActionLink(...) from my view to render an anchor tag with a link to an action. I know I can call RedirectToAction(...) from the controller to immediately call another action. But what I'd like to do (and don't know how), is get an action link from within the controller. I am building up a breadcrumb and want the link to an action. So I don't want to immediately jump to the action (as with RedirectToAction), but just get what the link would be.

View 3 Replies

MVC :: Can't Call Different Controller On View Using Action Link?

Feb 8, 2010

I have one problem with actionlink. My Example is: i have one view(xyz.aspx) and controller(abc.vb) , in this view i put below line

<%=Html.ActionLink("Manage", "Index", "Advertisement", New With {.aintCampaignid = Cam.CampaignID})%>

I have another view(Index.aspx) and controller(Advertisement.vb) if click on manage link in XYZ view it will call to advertisement controller and index function But iam getting [URL] it wont call.

View 5 Replies

MVC :: Controller Invokes GET Action Instead Of POST Action

May 20, 2010

I'm trying to add file upload functionality to a page. I've got a form that posts the selected file to a controller with a 'savefile' method. But if I don't add a get version of 'savefile' I'll get a 404 error. Here is the form code which is presented on the Index page:


And here is the controller code:


Intuitively I don't think I should need a GET version of SaveFile but if omit it I get a 404 error when the form posts. Why should I need a GET version of SaveFile when all I want is to post a form and save the file?

View 4 Replies

MVC :: Get The Current Action / Controller Name In A Controller Or Class?

Mar 14, 2011

How do you get the current action / controller name in a controller or class?

i can't show it in my view but that's not what i want.

View 1 Replies

MVC :: How To Test Action Which Alters Http Response

Apr 27, 2010

How can I unit test an action which alters the Http Response object? I haven't been able to solve this. I can mock the object but can't see how I can check the value after the action runs. I set the Response.StatusCode when errors occur in ajax requests and need to check it has been set in the unit test. The only alternative I can see is to change all my actions to return json objects with a IsSuccessful flag or similar.

View 5 Replies

How To Get The Id Value In Controller / Action / Id

Sep 24, 2010

I have an edit application view, and it can be found at the following URL:


1 equals the application ID.

Also on the page I have a link that goes to another view to add an assistant for the application. I want to pass it the application ID so that the new assistant can be "linked" to this application. How would I get the value of 1 and add it to my action link? This is what I have in my HTML so far:

<%: Html.ActionLink("Add New Assistant", "Create", "Assistant", new { applicationID = "id" }, null) %>

View 1 Replies

MVC :: One Action Per Controller?

Jul 9, 2010

If I have a controller with more than one action on, e.g. UserController, then I end up having that class require every dependency of every action. Is there a way I can have one controller per action and yet have the URLs appear the same way they currently do? e.g.MySite.com/User/Edit/1 would remap to User_EditController.Edit

View 1 Replies

MVC :: Null Action Id Returns System.NullReferenceException?

Oct 26, 2010

This is what I'm trying to run:


When I browse to www.mysite.com/mycontroller/myaction/myid, everything works ok. But visiting www.mysite.com/mycontroller/myaction produces:Server Error in '/' Application.Object reference not set to an instance of an object.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

View 3 Replies

MVC :: Unable To Unit Test The Custom Action Filter

Jul 17, 2010

I have a custom actionfilter that strips out the whitespaces before the html is rendered in browser and it is working fine. However I have been unable to unit test the custom action filter. Ideally I want to do an assert on the sample html that all whitespaces have been removed. Code looks something like this.


View 1 Replies

Same Controller Action Being Invoked In Mvc

Jan 12, 2011

I am not sure what is going on but even though I am invoking a different controller with action a particular FooController's Index action is being invoked all the time. The Global.asax file has setup the FooController to be the default controller.

public static void RegisterRoutes(RouteCollection routes)
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "FooController", action = "SomeAction", id = UrlParameter.Optional } // Parameter defaults

View 1 Replies

.net - Aspx Url To Mvc Controller Action

Dec 17, 2010

I have an aspx page Test.aspx. It handles requests like


How can I use routing to redirect this url to


I know I can create an aspx and perform redirect in it`s page load. But seems ugly and I think it can be done with some custom route.What I`ve tried was: this solution.but it didnt work for me. I remember, that Test.aspx should not be on a disk. I don`t have it, and routing is still not working.

View 1 Replies

C# - Securing Controller Action In MVC?

Feb 2, 2010

In ASP.NET MVC 2, to secure controller action, i have created a class RequirePermission inherited from ActionFilterAttribute class. The controller action looks like


Now instead of making different attributes , I want to use RequirePermission attribute like
[RequirePermission(permissions=Permissions.CanView+","+Permissions.CanEdit)] so that i can use it for different scenerious. but the compiler throw the following error. An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

View 1 Replies

C# - Retrieve URL For Action From Controller?

Dec 20, 2010

I am calling a Controller Action from a view, within that controller I need to invoke another Action which I will invoke to save the view to a network location as either HTML or Image.

How do I retrieve the URL to an Action from within a Controller. I need the actual URL, this means RedirectionToAction or View() wont work.

Why? I need to pass in a URL which will contain a call to a View. This view will be used to generate an image or HTML document using the System.Windows.Forms.WebBrowser.

.NET 3.5; C#; MVC 1;

I could do something like this, but its dirty ... well it leaves me with that dirty feeling.

using(Html.BeginForm("Action", "MyWorkflowController",
new {
MyId = "bla",
URLToGenerateImage = Url.Action("GenerateImage", "MyWorkflowController")

View 2 Replies

MVC :: Same Processing For Each Action Of A Controller?

Nov 2, 2010

I would like all actions of a controller to read some master page level values from the querystring and write those values to ViewState. How do I do this in ASP.NET MVC?

View 1 Replies

MVC :: Getting Controller And Action From HttpContext?

Mar 8, 2010

What's the correct way to get the controller name and action from an httpContext object? I can see the values in the debug, but I don't seem to be able to get to them in the object code?

View 13 Replies

MVC :: Get Current Controller And Action?

Mar 10, 2010

I have a user control in a modal popup for login form. It's ajax post so when login is successfull it doesn't update the rest of the page. Would like to just redirect the user to thier current page but not 100% on how to do this from the accountController.

View 4 Replies

MVC :: Use JQuery To Get Controller Action?

Oct 19, 2010

I have this action on below

public ActionResult Edit(int id)
var mtn0120 = __context.GetMtn0120(id);


View 4 Replies

MVC :: TDD Of A Controller.Action With An ActionFilterAttribute?

Jan 22, 2010

A well-known benefit of MVC is its suitablility for Test Driven Development (TDD) because you can directly call your controller actions from your test methods.

How can you test the combination of a controller action with a ActionFilter attribute (using OnActionExecuted to modify the ActionResult returned by the Action)? If I just call the Action, it returns the ActionResult from the action, but the filter attribute is never invoked. I think maybe you can get it by Controller.ActionInvoker.InvokeAction(controllerContext, "ActionName"), but you have to accurately mock so much of the controllerContext to make it work that it's a real pain.

View 2 Replies

MVC :: Action That Returns Viewresults For Ajax Client To Consume?

Dec 10, 2010

I have an action that will return partial view result if no error is found in the request , but when an error is found , how do I pass the error information back to client ?

Some error info now is in TempData["message"] , but some are model validation messages . The web client is using jQuery to make the ajax request (with $.ajax( ) call ) .

How does the jquery client distinguish between sucess or failure , and then acts appropriately to update a DIV that display TempData["message"] , and a DIV that consume the partial view result .

I was thinking perhaps a view-result can be encapsulated into a JSON result , and the JSON result will have fields to indicate the success/failure status , and the encoded markup of the partial view result .

View 3 Replies

MVC :: Calling Action Of A Controller From Another Controller?

Jan 24, 2011

I have a User entity and a department entity. I have a UserViewModel and DepartmentListModel which has List of departments.

UserViewModel has a property of type DepartmentListModel .

Now on user/create action I need to populate DepartmentListModel by calling DepartmentController's list action. List action returns populated DepartmentListViewModel.

From UserController how do I set DepartmentListModel ?

I tried doing so


But RedirectToAction returns RediretToRouteResult

View 7 Replies

MVC :: Why Synchronous Action Wasn't Executed Until Asynchronous Action Completed

Nov 29, 2010

I'm implement Comet in Asp.net MVC, I used timer to keep Async request in server, Async request will complete when timer elapsed 1 minute and response to client (to avoid 404 error) and then reconnect to Async Controller. I also wanna execute some Synchronous action during Async request was holding, but the problem is: When an Async action was executed and hold by using timer, the Sync Action wasn't called until Async action (comet long-live request) completed. I did test with firefox 3.6 many times, but the result is the same, so strange, Do you know why ? I have a sub some questions : To implement comet, using timer (response after some minutes elapsed) or thread (response after several time sleeping thread) to hold async request, which is better?

View 1 Replies

Copyrights 2005-15 www.BigResource.com, All rights reserved