When no record associated with the given id, which action should the action method do? Returning NoFound view or redirecting to NoFound action method? - asp.net

In Details, Edit, Delete action methods we have id parameter to retrieve the corresponding record from the database.
If no record corresponds to the id, we have 2 choices:
The executing action method returns a
specific view, which is usually named as
NoFound.cshtml under Views\Shared
directory, to inform the user that the
id is not valid.
or
The executing action method redirect
the user to a specific action method,
for example public ActionResult
NoFound (string message), to inform
the user about the issue.
My question is:
When no record associated with the given id, which action should the action method do? Returning NoFound view or redirecting to NoFound action method?
Edit 1
I need reasons from technical point of view such as security and performance.

It's all personal preference. In my opinion though, the real thing that should happen in this situation is a 404 response code should be sent back to the client. You can have custom 404 pages that give you the best of both worlds.

Refer to Step 5 on this answer - custom 404, proper 404 response code, proper handling of the "item not found" case.

I would return a "Not found" view, because this way an URL with ID could be stored in favorites and will became valid when record with this ID will be available.

I would recommend still returning a 404 error. The order of the parameters in the route is irrelevant, some people prefer {controller}/{action}/{id}, in some cases {controller}/{id}/{action} might make more sense. In either case if all three parameters are required for it to be considered a valid request and one is missing you should return a 404. It will also make testing your site much easier as if you return a view it will have a result code of 200 and make it much more difficult to track down broken links. 404 errors are universally recognized as a problem when doing site analysis.

Related

Spring MVC retain error meesage after redirect

I have a jsp with 2 input boxes that searches on 2 different scenarios. So , i have two different Get requests mapping to /search1 and /search2 but have created only one POJO for this.
As soon as user submits a search, I check if there is any results for that, and if not i add error message in model and "redirect:"(redirection because if i simply return a page and now make a new search the url will be /search1/search2) to the same basic page.
But everytime i load the page the error message persists.
Any workaround for this? how do i display the messaeg only on search.
Use Flash Attributes.
http://viralpatel.net/blogs/spring-mvc-flash-attribute-example/
Add RedirectAttributes parameter to your controller's handler method.
then redirectAttributes.addFlashAttribute("modelAttributeName", "value");
then redirect
The controller handler that you redirect to should have "modelAttributeName" model attribute available to it.
Essentially your a putting a value into session which is removed as soon as it is read on the next request.
Another solution would be
you can use request parameter to pass message id and write utility to read message from properties file using message id in the get request of redirected controller.
Controller 1
return "redirect:your_redirection_url?messageId=1";
Controller 2 (where redirected)
if (messageid != null && !(messageid.equals(""))) { MessageUtility.addMessage(Integer.parseInt(messageid), model, locale); }

Symfony2 dynamically select controller based on query result

I'm trying to create an extendible cms. I have URLs in my database like "/menu-item/content" for each of these urls there is an action specified in format "AcmeDemoBundle:Default:index".
I would like to resolve this action and call the appropriate controller based on the matched URL.
My main goal is to call the controller stored in the db directly. Before i used a method when i put a route in the end of the routings which matched all urls associate a controller with it that found out the target controller from the db, than used Controller::forward() to get the expected results. The main problem with this approach is that i need to create an unnecessary subrequest for each page load. The other problem is passing post variables to this new subrequest.
So far i've tried to listen on kernel.request event and modify $request->attributes to contain the matching _controller value, but it seems like it has no effect.
Any advice in the subject is appreciated.
I think you mixed up the forward and redirect calls. Redirect performs 301 or 302 redirects (permanent and temporary respectfully), which creates the subrequest, as you call it. Forward performs internal call (same request) to the new action. As for your second problem, as forward is an internal call within the same request, everything global stays the same (including POST variables). In my opinion, using forward might be a good solution for you (if you don't have any other low-level requirements or if you want to be even more flexible than the usual MVC operation allows).
Hope this helps.

ASP.NET MVC rendering common data such as notifications

In my requirement, all views should be able to display notifications based on what happened within Actions in the controllers. The implementation as it stands is in a BaseModel which is inherited by all Models needing to show notifications. Then, any view with this requirement simply calls:
#{ Html.RenderPartial("MessageCtrl", Model); }
And a notification is rendered. This works fine in cases where an Action is returning a view directly, and is usually handled like this in an Action:
model.SetMessage(response);
return View(model);
...where SetMessage updates the notification related properties of the model from an object returned from my service. All responses implemented an interface I created called IResponse, so messaging can be handled throughout.
The problem arrises when I need to return a View from a different Action and still show a notification.
For example, on my EditUser action, if edit was successful, I want to show the Index view. To achieve this from the EditUser action, I need to return RedirectToAction (because simply returning the view will return the correct view with the incorrect URL indicating it is EditUser when it is not). However, RedirectToAction loses all notion of my model and BaseModel, so by the time it redirects, it has lost my notification. I know I can use RouteValues, but that defeats the purpose of having a common base model driven notification approach. I even looked at the ViewBag (usually I dismiss it as the ugly part of MVC), but found that ViewBags only persist for the life time of a single request, and using RedirectToAction, the ViewBag is empty again by the time it hits the target Action. Another thought was to store it in a session object, but that sounds downright ugly to me.
This seems like its a pretty standard requirement. Is there a standardised approach to passing common data (like notifications) around to different views asside from through the querystring through a RedirectToAction?
I think you are looking for TempData which only persists data in session across one redirect.

Changing view vs Redirecting in ASP.net MVC3

There are several advantages of having doing a GET_POST_REDIRECT after an operation; however in absence of making a redirect, we return a View i.e. return a different view (after POST return an Index view).
In terms of web and pages what is the real thing going on when we return a different view from the action method (Returning index from POST). When can we use this and when it isn't recommended. What happens to the URL / URI?
If in case of single big/sophisticated page which requires several levels of processing should we redirect to same page again with error or returning the same view with error message, which one will be better or recommended?
You can return any ActionResult from action. You can return ViewResult for view with explicitly specified name (which differs from action name). Nothing happens with page URL in such case just a different HTMLs will be returned from the server for different views.
Use redirect or return result directly - it depends on your actual case and web site design. There are no any concrete answer.

ASP.NET MVC - How To Refresh View From Actionmethod as a response to request?

I have an action-method in a controller that takes requests coming from a variety of different views.
It is somewhat of a utility method and I simply want it to accept the parameters it is given - do something - and then refresh the view that sent the request.
Right now, the only way I see to do this is by having the method figure out what view sent it the info and do a:
return RedirectToAction("method", "controller");
For each possibility (or something similar to that).
Is there a more general way I can make my method just re-render the current view without having to explicitly identify it?
-Thanks
Your best bet is to use jQuery to post the data then utilize the results as you see fit. Otherwise you can pass in the action/controller name in the post and use them dynamically to redirect.

Resources