Why can't I set a breakpoint in a ASP.NET view? - asp.net-mvc

If I set a breakpoint in the compiled code (for instance in an action), I can then step through, and eventually am stepping through the generation of the View.
I've found this useful a couple of times, but it's tedious as you have to step through a lot of code to get there.
However, I can't set a breakpoint in view. I just receive the message 'This is not a valid location for a breakpoint' from VS2008. Why is this?

Right click on the code you want to break on and go to "Breakpoint -> Insert Breakpoint".
Why? Must have something to do with the face your not working with a pure code file and hitting the left sidebar doesn't know which block of code to use.

Another thing that comes to my mind is to use the debugger; keyword in the view, though I am not 100% sure it will work as in ASP.NET ajax site.

Click to the code where you want to debug and than press F9.

UpTheCreek,
Not really a 'nice' way to do it, however, you can insert some 'flip-flop' code in your view that CAN have a breakpoint placed in it and then step from there. This code doesn't neccessarily have to perfom a function (tho obviously, would be useful if it did).
anyway, you don't want the crumbs, so here's the (meal) deal:
<%
int rspId = 0;
string uniqPageId = Guid.NewGuid().ToString().Replace("-", "");
%>
then, simply place the breakpoint at the side of any of those variable definitions.
Not clean and definately a 'jfar' markdown candidate :-)

Related

Vaadin23 vaadin.eagerServerLoad=true and BeforeEnterObserver

I switched to vaadin.eagerServerLoad=true in order to fix the issue with incorrect HTTP status code (I need to return 404 when it is needed).
I also noticed one issue which potentially maybe related to the vaadin.eagerServerLoad=true. On the views with com.vaadin.flow.router.BeforeEnterObserver the method BeforeEnterObserver.beforeEnter is executed twice when I for the very first time enter the page (please note that there is no such issue when I navigate between views in the already constructed UI). This is why now as a temporal fix I have to call removeAll() as a first line in the beforeEnter method.
What am I doing wrong and how to fix it?

Vaadin23 navigate is not working in BeforeEnterEvent

I try to check if specific parameters of a url are valid. If they aren't the user should be navigated to a different route. It's important that it's a navigate and not a redirect.
When i try to use UI.getCurrent().navigate() in der BeforeEnterEvent i'm experiencing some weird issues.
Some workarround i found is when i call the navigate method in the UI.getCurrent().access() function. Since then the code will be executed at a later point.
UI.getCurrent().access(() -> UI.getCurrent().navigate(""));
So i'm wondering if this is the right way to do this because this seems pretty bad. Is there a better event to call the navigate method?

Examine HTML output when debugging MVC Views

Is there a way to view what HTML has already been produced by an MVC Razor View when stepping through it with a debugger?
For example, in a view such as the following:
...
#Html.Raw(someString)
#Html.Partial("_SomeCommonComponent")
#Html.Raw(someOtherString) <---------- Execution broken at breakpoint here
...
Is it possible to see the combined output of just the first call to 'Raw' and the Partial view?
The reason I ask is that I have several complex calls together, (some which produce no output at all) and one of them is inserting incorrect characters, and I'd like to be able to find which call is the culprit by stepping over the code until the incorrect character has been output.
In Visual Studio you could put a break point on a code line in the view, then add a watch on both the Html.Raw(somestring) and Html.Partial("_SomeCommonComponent") method calls. You should then be able to see the HTML values in the watch window.
Also try and watch the ViewContext.Writer property in the view.

How can I prevent the user from navigating back to a previous page?

I have an ASP.NET MVC application, with three views: view1, view2, view3. The logic way for the user to navigate through these is: view1 -> view2 -> view3.
When the user reaches view3, then I must prevent them from loading view2, even by using the "Back" button in their browser.
What is a good, browser-independent means of implementing this?
In most of the applications you have to cope with the back ability from the browser. The user is used to it and he wants to use it and he more or less will hate pages that try to trick them when going back and forward.
Don't try to fool you user think about what he wanted to do and then try do deliver a not completely broken page.
Add a check of referrer page on page load in your application and then show a page or redirect user back to used view. You cannot manipulate or disallow basic navigation on client, but you can solve this problem server-side
I can't comment on the earlier posts, but note that some browsers don't pass referrers, and thus the earlier solution would break (throw an exception, actually).
There are two steps to this:
1) You have to prevent browser-side caching. If you've got a three step process that the user walks through and it's dynamic, you're probably already doing this. If you don't prevent caching, the back button will show the cache of view1. Since step 2 is done server-side, the server won't have a chance to do anything.
2) You need to, as previous poster's have said, do something on the serverside to prevent the display. There are two ways to do this (despite my really bad pseudo code).
a) The quick & dirty way is based on the referer. For example, you'd put the following check on the controller for view2:
if (request.urlreferrer.absolutepath == "controllerview1")
{ //good }
else
{ //bad }
Also, in the case of "bad", you'll have to consider what to do. If you're using forms to pass values back and forth, you've suddenly lost when the user goes back to view2.
Note, though, that some browsers don't ever pass referrers and the above check won't do any good (and request.urlrefferer will be null). (I believe this is generally due to firewalls.) In which case you'd have to do:
b) I've done something like this before. The controller view1/2/3 is essentially a wizard where they're walking through the system. Each controller updates the db row associated with the wizard. So, view 2 would do something like:
if (dbrow.last_saved_page_num == 1)
{ // good }
else
{ // bad
redirect("view" + dbrow.last_saved_page_num + 1);
}
That is outside of the scope of javascript, and cannot be disabled (though you can tell the browser to go forward or back, you cannot prevent it). You would need a server side solution to disallow access to the pages.
There is no JavaScript solution, it would have to be implemented server side.
You can add a function to view1 and view 2 that fires on page load, if you use jquery something like :
$(document).ready(function(){ window.history.forward(); });
or you could just just do this
<html onload="window.history.forward()">
Either way will do pretty much what you want, but maybe not be the best solution in terms of user experience - but if its all you have to work with then it might be the best solution.

ASP.Net MVC - strange params caching behaviour in Actions

I'm facing a strange problem in my project. My actions are getting old param values instead of the actual values which are in Request.Params. I created a HomeController.Echo(string text) action to illustrate it (see screenshot). When I call the action for the first time like "Home/Echo?text=aaa" everything works fine. When I call the same action second time with different text value ("Home/Echo/text=bbb"), I get the old "aaa" value again in my action "text" parameter. Strange think is that Request.Params contains the right "bbb" value.
I'm thinking if there's something I could break myself, but can't figure out anything. I'm serving controllers from IoC container, I overrided ControllerActionInvoker.InvokeActionMethodWithFilters method (to inject dependencies into filters from IoC) and I'm handling HttpApplication.AuthenticateRequest. Im'not working with params/binding anyhow in any of these...
screenshot
The problem was caused by some threading issues probably - I forgot to register controllers in my IoC container with per-request lifecycle (they were registered as singletons).
Have you debugged through the application to see where the value is getting switched out. A simple watch on the text variable (whatever you call it in the code) should yield where the variable gets changed. Without code to run through, I have no clue where it is happening.
I would say write a test, but there is still a possibility of UI interfering here. If you find where in the code it is changing, then write a test to confirm the bug and start whacking at it.
I suggest you to start commenting all the methods you overrode until you isolate the problem. In worst case you will get to the point where the ASP.NET MVC wizard left your project when you created it and where parameter binding definitely worked.

Resources