How to send debug text from C# class to ASP.NET page - c#

I am working on a C# class that is a part of my ASP.Net Web Site.
Is there a simple way to output some log/debugging text to the top of the page. My class does NOT inherit from Page. I want to display variable values, etc.
The class represents an Exam object that I use in some of my aspx pages. The variables that I want to display are private and therefore inaccessible to my aspx pages.

Anywhere in the context of an http request you can reach the current executing page as follows.
Page page = HttpContext.Current.Handler as Page;
You are free to cast to your own page type. So you can write debug info to labels, textboxes etc.

you can just do this
HttpContext.Current.Response.Write("your message goes here");
or write a helper method
public static void writeOut(string message) {
HttpContext.Current.Response.Write(message);
}

You can try with Response.Write method
var pathOfYourLog = "";
var log = File.ReadAllLines(pathOfYourLog);
YourHttpContext.Response.Write(log);

Related

How can I tell FindsBy to search elements on this particular sites

I've started learning Selenium Webdriver. It seems pretty easy but I'm not sure how to use PageObject Pattern. I understand the idea, I guess, but I don't know how to map many pages in one project. For example: the site contains many subsites (i.e. login page, create an account page) and I'd like to create PageObject for each of them. When I execute my script, I'm getting messages that elements haven't been found. What should I do? Should I create a separete drviers or what?
Seems that you are doing everything correct: create a new object for each page. Create different objects for common elements that are the same on a lot of pages (like footer, header menu, etc.).
Then to combine page objects, first you need to start somewhere. For example from you main page. So use the MainPageObj to interact wit the main page. As soon as you navigate to another page you need to use a method in your MainPageObj class.
This method must return a page object of the new page. So if you open a Login page from the main page then your method OpenLoginPage() should return LoginPageObj.
Example: MainPageObj and LoginPageObj are page object classes.
MainPageObj class has method:
public LoginPageObj OpenLoginPage()
{
LoginButton.Click();
return new LoginPageObj();
}
So use this method in this way:
MainPageObj mainPage = new MainPageObj ();
LoginPageObj loginPage = mainPage.OpenLoginPage();
Simple?)
If you want to make it more beautifyl then add static class Pages and add static methods for each page object you have to this class. Each method must return a new instance of your specific page object.
Example:
public static class RealSites
{
public static class Kinopoisk
{
public static Kinopoisk.MainPage MainPage
{
get { return new Kinopoisk.MainPage(); }
}
}
}
And in your tests use it this way:
Pages.RealSites.Kinopoisk.MainPage.OpenMoviePage(url);
Pages.RealSites.Kinopoisk.MoviePage.DoSomethigElseMethod();
You can refer this link to understand page object model and after this you can also create for your application
http://www.seleniumeasy.com/selenium-tutorials/simple-page-object-model-framework-example
If you want to learn then selenium easy is best site to learn for beginners.

Inject JavaScript code from asp.net page into html page in another domain

How can I inject a JavaScript code from asp.net page into html page in another domain e.g http://www.codeproject.com/ . how to inject a JavaScript into this html page from my application
I am currently working on making a plugin just like Pinterest when the html page opens from my application it shows a bookmarklet just like Pinterest automatically on the page.
Below is the code I am using to inject JavaScript
public partial class ViewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string script = "javascript:(function(){var jsdom = document.createElement('script');jsdom.src = 'http://localhost:15064/Script/delete.js';document.body.appendChild(jsdom);})();";
Response.Redirect(Server.UrlEncode(script));
}
}
Below is the Error I get After the execution of above code
Same Original Policy will not allow you to run scripts in someone else's domain.If this did not exist anyone could run scripts in any domain which would be a major security risk.
There are a couple *legal exceptions to this rule which you can read below
https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy
The only other way to accomplish outside of an agreement with both sides would be a violation of Same Origin and probably illegal.
Xss attacks are pretty common and do what you are describing.
The closest you will probably be able to get is a bookmarklet:
http://www.mattcutts.com/blog/javascript-bookmarklet-basics/
which is basically a shortcut to inline javascript.
An example in the wild is the X-Ray Goggles bookmarklet. You can add it by creating a new bookmark and pasting the location as:
javascript:(function(){var script=document.createElement('script');script.src='https://goggles.webmaker.org/en-US/webxray.js';script.className='webxray';script.setAttribute('data-lang','en-US');script.setAttribute('data-baseuri','https://goggles.webmaker.org/en-US');document.body.appendChild(script);})();
This won't let you inject from your application but it is a way of injecting into a doc from your browser.

Central page for error messages

In my ASP.NET C# application, there are many different error messages that I'd like to display.
The way I display my error message is by pop by via:
Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('My error message here');", true);
return;
Since there are many different error messages (some repeated across pages) and I have so many different pages in my application - I'd like to put all the error message on a centralized page and reference to it somehow - so that when I need to change my error message, I only need to change in one page and not across ALL the pages.
What's the best way to do this?
I imagine I need to create a .cs page? And have a different ID for each of the error messages.
This seems like a very simple thing to do but I am a bit lost as to how to start on it.
Can someone advice the best way to do this?
Thanks.
I would add extension method to show the alerts and keep all the exception strings in resource file. then I can call the method as below
USAGE
this.ShowAlert(Resource1.MyException);
EXTENSION METHOD
using System;
using System.Web.UI;
namespace WebApplication1
{
public static class Extensions
{
public static void ShowAlert(this Control control, string message)
{
if (!control.Page.ClientScript.IsClientScriptBlockRegistered("msgbox"))
{
var script = String.Format("alert('{0}');", message);
control.Page.ClientScript.RegisterStartupScript(control.Page.GetType(), "msgbox", script, true);
}
}
}
}
Add resource file to your project and enter messages as string entries with meaning full names.
Since you appear to display JavaScript error messages I would create a JS include file and define the JS methods there. That way you can reuse the methods on all pages that include the JS file.
https://softwareengineering.stackexchange.com/questions/91242/what-is-the-best-way-to-include-javascript-file-using-script-tag
JS method can be defined in
* the Master Page
* in the custom base class inherited by all the pages.

Get Page's Type from URL in C#

Given a URL, I have to be able to know what the page's Type is that resides at that URL. For example, lets say I have a couple pages.
//first.aspx
public partial class FirstPage : System.Web.UI.Page { }
//second.aspx
public partial class SecondPage : MyCustomPageType { }
I would like to be able to call a method in the following manner with the following results:
GetPageTypeByURL("first.aspx"); //"FirstPage"
GetPageTypeByURL("second.aspx"); //"SecondPage"
Is this possible? How can I do this?
From this answer, it appears that you can get the class of a specific page. You may then be able to use reflection to determine its base type. (Note: I haven't attempted this, it's just a suggestion.)
System.Web.Compilation.BuildManager.GetCompiledType(Me.Request.Url.AbsolutePath)
What about this?
public Type GetPageTypeByURL(string url)
{
object page = BuildManager.CreateInstanceFromVirtualPath(url, typeof(object));
return page.GetType().BaseType.BaseType;
}
Usage:
Type pageType = GetPageTypeByURL("~/default.aspx");
Just a thought:
I assume that you calling the page from some other program.
Get the HTML, search for your distinguishing HTML / hidden element that tells you what the page is.
If you are on the server just load up the page as a text file and read it.
Page page = (Page)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(url, typeof(Page));
CustomerPage page = (CustomerPage) SomeMagicalPageCreater.CreatePage("CustomerPage.aspx");
https://forums.asp.net/t/1315395.aspx
This is what I found.

How to use Skin.AddPageMessage() method?

I am developing a DNN module and I want to display an info message at the top of my ContentPane, not above the actual module. I have found that DotNetNuke.UI.Skins.Skin.AddPageMessage() should just do the thing. I am not getting the behavior I want though, the message just won't display at all.
There are few overloads of this method, one group accepting a Page object, the other one taking a Skin object.
public static void AddPageMessage(Page page, string heading, string message, ModuleMessage.ModuleMessageType moduleMessageType)
public static void AddPageMessage(Skin skin, string heading, string message, ModuleMessage.ModuleMessageType moduleMessageType)
I did take a look into the DNN source and found out that in the end they're actually using the same private static AddPageMessage(...) method, which just looks for a ContentPane within the provided control and adds a new ModuleMessage to the collection of its controls.
What should I pass as a Page or Skin parameter to get this correclty working?
Thanks ...
The private AddPageMessage method takes a fairly ambiguous "Control" as the first parameter. I believe that needs to be the current Skin, as it does a FindControl for ContentPane.
Doing something like this should get you a reference to the current skin:
var skin = Skin.GetSkin((PageBase)this.Page);
Skin.AddPageMessage(skin, "Header", "Message", ModuleMessageType.GreenSuccess);
the reason why the messages are not showing up is that you turned on "enable partial rendering" in the controlssetting of the modulecontrols.
If you are using AJAX (this is happening if you set the partial rendering to true) the DNN modulemessages are turned off from DNN itselfe.
Its enough if you have turned the partial rendering on just 1 control (dont have to be your control where you are acting from) on your page. DNN will wrap the whole page into ajax script manager and messages are not working anymore.
*EDIT 26.04.2012 10:45:
You can get the current ScriptManager by executing the following Code for example in you Page_Load(). If the manager is null, you dont have ajax enabled and the modulemessages should work. If bIsAjaxEnabled is true the modulemessages are disabled.
ScriptManager manager = AJAX.GetScriptManager(Page);
if (manager != null)
{
bool bIsAjaxEnabled = manager.SupportsPartialRendering;
}

Categories

Resources