Reading ?= arguments to dynamically set debug mode - asp.net

I was wondering how to read the arguments from a url, ie. localhost:12345?debug=true and be able to set the debug mode. I originally wanted to use the same controller I use by default, however I got an error when I tried to set #define DEBUG because it wasn't at the start of the file, which means I need a separate controller to do this, but how do I check to see if ?debug=true is there, and to check if debug is set to true rather than false?

You are making too much work for yourself. In your web.config, you can set the compilation to be in debug mode (or not)
<compilation debug="true">
If this is set in your web.config, use the framework to tell you if you are in debug mode or not. If the above line (debug="true"), then the HttpContext.Current.IsDebuggingEnabled will return true.
if (HttpContext.Current.IsDebuggingEnabled)
{
DoSomethingDebuggy();
}
else
{
DoSomethingElseCompletely();
}
Now, you no longer need to append a random query string for debug mode (which I am sure will make your routes and links happier). However...if you really really want to keep that query string, then in your controller/action, you can use the following:
public ActionResult Home(){
var debugParam = Request.QueryString["debug"];
//be sure to check for null or empty string before casting to a bool
}

So because in my project, in the .cshtml file, I was using razor to check to see if the debugger was set, I just added an extra argument, || Request.QueryString["string"] == "value" and it solved my problem. A one line fix, where string is the name of the argument you want to look for.

Related

In gatling, how do I validate the value of a string extracted via the css check?

I'm writing a Gatling simulation, and I want to verify both that a certain element exists, and that the content of one of its attributes starts with a certain substring. E.g.:
val scn: ScenarioBuilder = scenario("BasicSimulation")
.exec(http("request_1")
.get("/path/to/resource")
.check(
status.is(200),
css("form#name", "action").ofType[String].startsWith(BASE_URL).saveAs("next_url")))
Now, when I add the startsWith above, the compiler reports an error that says startsWith is not a member of io.gatling.http.check.body.HttpBodyCssCheckBuilder[String]. If I leave the startsWith out, then everything works just fine. I know that the expected form element is there, but I cant confirm that its #action attribute starts with the correct base.
How can I confirm that the attribute start with a certain substring?
Refer this https://gatling.io/docs/2.3/general/scenario/
I have copied the below from there but it is a session function and will work like below :-
doIf(session => session("myKey").as[String].startsWith("admin")) { // executed if the session value stored in "myKey" starts with "admin" exec(http("if true").get("..."))}
I just had the same problem. I guess one option is to use a validator, but I'm not sure how if you can declare one on the fly to validate against your BASE_URL (the documentation doesn't really give any examples). You can use transform and is.
Could look like this:
css("form#name", "action").transform(_.startsWith(BASE_URL)).is(true)
If you also want to include the saveAs call in one go you could probably also do something like this:
css("form#name", "action").transform(_.substring(0, BASE_URL.length)).is(BASE_URL).saveAs
But that's harder to read. Also I'm not sure what happens when substring throws an exception (like IndexOutOfBounds).

Sharepoint If-Modified-Since

I wrote a handler to be used within SharePoint that will generate a JSON string from a given parameter in the query-string. This JSON string will then be used by a JS function to display the relevant data in HTML to the client, however, the call is somewhat costly and would like to cache the output once.
The handler currently caters for everything regarding OUTPUT cache and tested within an application bares fruits, however, I'm baffled by the fact that, specifically in SharePoint (2007) the "If-Modified-Since" header attribute never appears, basically it always comes back as null.
I found blog-on-blog that discuss this in length in regards to images, and include files but I can't find anything specific regarding this with pages (ASPX, AXD, ASHX) and the handler self.
My only assumption here is the fact that I'm using an AXD file, which is not directly supported by OUTPUT cache by default?
The code looks something like:
bool isModifiedSinceLast = (context.Request.Headers.Get("If-Modified-Since") != null)
: true
? false;
if (!isModifiedSinceLast)
{
context.Response.Headers.AppendHeader("If-Modified-Since", Guid.NewGuid());
}
else
{
// complete the call from cache
}
Thanks,
Eric

return domain objects from grails constraint validation

Is it possible to write your own validator in grails that will return a valid object?
Something like:
static constraints = {
name(validator: {val, obj ->
if (Drink.findByName(val)) return [Drink.findByName(val)]
})
}
In other words - if the Drink already exists in the DB, just return the existing one when someone does a
new Drink("Coke")
and coke is already in the database
You cannot do this with a custom validator. It's not really what it was meant for. From the Grails Reference:
The closure can return:
null or true to indicate that the value is valid
false to indicate an invalid value and use the default message code
a string to indicate the error code to append to the "classname.propertName." string used to resolve the error message. If a field specific message cannot be resolved, the error code itself will be resolved allowing for global error messages.
a list containing a string as above, and then any number of arguments following it, which can be used as formatted message arguments indexed at 3 onwards. See grails-app/i18n/message.properties to see how the default error message codes use the arguments.
An alternative might be to just create a service method that 1) looks for the domain and returns it if it exists, 2) otherwise, saves the domain and returns it.
There's probably a more elegant alternative. Regardless, Grails' constraints mechanism isn't (and shouldn't be) capable of this.
Not sure if you can do this from inside the validator, but:
Drink d = Drink.findOrSaveWhere(name: 'Smooth Drink', alcoholLevel: '4.5')

Access compilation element in web.config [duplicate]

Is there any way to access the <compilation /> tag in a web.config file?
I want to check if the "debug" attribute is set to "true" in the file, but I can't seem to figure out how to do it. I've tried using the WebConfigurationManager, but that doesn't seem to allow me to get to the <compilation /> section.
Update:
I know I could easily just load the file like an XML Document and use XPath, but I was hoping there was already something in the framework that would do this for me. It seems like there would be something since there are already ways to get App Settings and Connection Strings.
I've also tried using the WebConfigurationManager.GetSection() method in a few ways:
WebConfigurationManager.GetSection("compilation")// Name of the tag in the file
WebConfigurationManager.GetSection("CompilationSection") // Name of the class that I'd expect would be returned by this method
WebConfigurationManager.GetSection("system.web") // Parent tag of the 'compilation' tag
All of the above methods return null. I'm assuming there is a way to get to this configuration section since there is a class that already exists ('CompilationSection'), I just can't figure out how to get it.
Use:
using System.Configuration;
using System.Web.Configuration;
...
CompilationSection configSection =
(CompilationSection) ConfigurationManager.GetSection( "system.web/compilation" );
You can then check the configSection.Debug property.
TIP: if you need to know how to get a value from a config file, check the machine.config file in your \Windows\Microsoft.net\Framework\<version>\CONFIG folder. In there you can see how all the configuration section handlers are defined. Once you know the name of the config handler (in this case, CompilationSection), you can look it up in the .Net docs.
The easy way to check if you're running in debug mode is to use the HttpContext.IsDebuggingEnabled property. It gets its answer from the compilation element's debug attribute which is the same thing you're trying to do.
After all, you can always load up the Web.config file into an XmlDocument and use an XPath query to find out!
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Web.config"));
doc.SelectSingleNode("/configuration/system.web/compilation/#debug");
However, I suggest you use the Configuration.GetSection method for solution.
CompilationSection section =
Configuration.GetSection("system.web/compilation") as CompilationSection;
bool debug = section != null && section.Debug;
You can always check debug option on the compiler level by enclosing your code with
#if (DEBUG)
#endif
in case that was the idea...
Try using:
HttpContext.Current.IsDebuggingEnabled
Cant you just load the file up as a regular XML File and use XPath to get the nodes?
Try using the ConfigurationManager.GetSection method.

HttpContext.GetGlobalResourceObject always returns null

I created two files in the App_GlobalResources folder:
SiteResources.en-US.resx
SiteResources.sp-SP.resx
Both contain a value for "SiteTitleSeparator".
Here is what I am trying to do (The following line always returns null):
string sep = (string)GetGlobalResourceObject("SiteResources", "SiteTitle");
Note, that the Culture property on the page is set.
Answers in both VB and C# will be welcomed.
I changed the name of SiteResources.en-US.resx to SiteResources.resx and now everything works just fine.
Seems theer must be one invariant resource.
Yes: there has to be one .resx without a region code which will serve as a default.

Resources