I have a published project which is used by side company and which can not be updated now. The application got quite some changes which should not be published to this certain URL. Instead on one aspx page.
Question - how can I create DLL for one single aspx page inside big project so I will be able publish only it.
Thank you
Encapsulate all logic in a class library :
public class MyBusinessLogic
{
public bool DoSomethingWithFormData(string val1, string val2)
{
//do your logic here...
}
}
And create your Aspx page , in the code behind use :
protected void YourButtonOnPage_Click(object sender, EventArgs e)
{
MyBusinessLogicm = new MyBusinessLogic();
m.DoSomethingWithFormData(txt1.Text,txt2.Text);
}
In this way you have to publish only a dll and an aspx page.
Note that the code is not clean.. don't pass textbox data to business in this way but do some validation first.
Related
A function needs to be called from every page, this function is a Web Method. Its in the Roor.Master Page now how can it be called from all pages using AJAX. Since there are many pages creating same function or a function which calls this function in turn is hectic.
If you have common functionality, that is shared between most or all of your WebForms, you can always create a Base page class, which inherits from Page. All other pages, that are derived from your base class, would then contain that method:
public class MyBasePage : Page
{
[WebMethod]
public static void DoStuff()
{
//Do your work here
}
}
No, whenever you need that method, you can just inherit your aspx webform from MyBasePage instead of Page:
public partial class Foo : MyBasePage
{
/*...*/
}
Since you can also use WebApi, MVC and WebForms side by side in the same project, exposing this functionality through a simple ApiController could be feasible as well, if you do not need to access webforms specific elements, like variables that are defined your Base page oder master page.
There is no physical existance of pagemethod after the page is rendered. So, jquery ajax call is not possible to a page method in master page. But following code looks impressive to me.
http://weblogs.asp.net/yonggangmeng/archive/2008/11/13/how-to-invoke-pagemethods-in-masterpage.aspx
I've a site, we are using ASP.NET 4.0, and right now our products content is managed like this
www.franko1.com/products.aspx?serie=2000
where the querystring serie is the product ID, so its value is taken and then the contents is extracted from the database.
Now for SEO reasons, we've been asked to change the urls, so now they have to look like this:
What the boss want | Current Urls
--------------------------------------------------------------------------------
www.franko1.com/Relief_Valves | www.franko1.com/products.aspx?serie=2000
www.franko1.com/Inline_Flame_Arrester | www.franko1.com/products.aspx?serie=1000
www.franko1.com/Vent_Hatch | www.franko1.com/products.aspx?serie=3000
and so on ...
Right now, we are using a masterpage and the products.aspx and as I said, we take the querystring serie and we show the content based on its value, I have no idea how to do this using asp.net, I have read about ISAPI_Rewrite but I was wondering if there is a technique to approach this without dealing with the IIS server....
Well I don't know if I was clear, It is hard to explain,
No need for that. You can achieve this via routing (It's not just for MVC).
Routing has been available as a stand alone module for a while now, but with ASP.Net 4.0 you can now use routing for WebForms just as easily as you can with MVC.
You will need to add some routing to your Global.asax
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("Products", "Products/{Name}", "~/Products.aspx");
}
}
And with that you can now reference the route values in your page like so:
protected void Page_Load(object sender, EventArgs e)
{
string prodName = Page.RouteData.Values["Name"].ToString();
//Do lookup, etc...
}
Your URLs will end up looking like this:
www.domain.com/products/Relief_Valves
www.domain.com/products/Widgets
www.domain.com/products/TrashCans
etc..
Nice and easy... and clean!
URL rewriting was made just for exactly this purpose. The problem is that it really doesn't work with variable content directly appended to the root URL. There really isn't enough information for URL routing to separate such URLs from the rest of your URLs.
Would your boss accept www.franko1.com/p/Relief_Valves ?
Within my ASP.NET App I am using a Method to check user permissions to decide whether the user can view the page, or get redirected to the "Invalid Permissions" page.
Since adding this Permission Redirect on the Master Page would cause an infinite loop, I am forced to apply it to all pages. I do not want to have to copy this Method onto every page, so I would like to create a class within my Web App which holds this Method so I can use it globally across my app.
I have had no formal training, but my gut is telling me that it's bad practice to place a Response.Redirect or any "Web" functions in a class? Am I correct? And if so, is there a better way to go about this?
You can check the current url to make sure that it is not already the invalid permissions page before redirecting; therefore, you will only redirect when you are not already there.
if(!Request.RawUrl.Contains("Invalid Permissions Page"))
Response.Redirect("Invalid Permissions Page");
You can make a new class, let's call it myPageClass, that heritage from System.Web.UI.Page, then, include all the code you need in this class make all your code behind heritable from myPageClass.
public class myPageClass : System.Web.UI.Page
{
public void authorize()
{
// your auth code here
Response.Redirect("Invalid_Permissions_Page.aspx", false);
}
}
public partial class _Default : myPageClass
{
protected void Page_Load(object sender, EventArgs e)
{
// Your code here
}
}
In my opinion, you should not use Response.Redirect in the cases of the action of a button, for example, if it's going to take you to another page, you don't need to go to the server to do that, that's only to be made in the client.
First things first, your original question:
Bad practice to have a Response.Redirect within a class in ASP.NET?
Yes I believe this is a bad practice. It's safer to pass a delegate, then your 'AuthorizeRequest' method can call the delegate. Here is an example:
public static void AuthorizeRequest(Action<string> redirect)
{
if( /*whatever*/ )
redirect("/InvalidPermissions.htm");
}
protected void Page_Load(object sender, EventArgs e)
{
AuthorizeRequest(Response.Redirect);
}
Now the bigger problem... You do not want to do this!
Having each page assert authorization is a quick way to write security issues. Someone will forget or accidentally remove the assertion. ASP.NET has a multitude of ways to intercept and filter requests for this very purpose.
The easiest thing to do is to place this in event hooks in your Global.asax file. The HttpApplication object has several events that can be used for this purpose. Another option is to implement the IHttpModule interface. Either way, I would not write the code in each page.
I want to build a webpage containing jquery draggables in asp.net (c#).
How can I add my jquery code to my page in an object oriented way?
So I want to make a c# myDraggables.cs class that I add to the asp page with the htmlgenericcontrol. How do I add this jquery code (eg: $('#mydraggables').draggable(); ) in my class.
I could do it with the htmlgenericcontrol by adding a "script"-tag. But isn't there a way to do it with a jquery helper (can't find that)?
tx
A sample:
Basicly, in my myDraggables-class I could have;
public HtmlControl Draw()
{
HtmlGenericControl aDiv= new HtmlGenericControl("div");
aDiv.Attributes.Add("id", "dragtest");
return aDiv;
}
public HtmlControl DrawJquery()
{
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.InnerHtml = "$('#dragtest').draggable();";
return js;
}
And the code behind of my asp.page would look like;
protected void Page_Load(object sender, EventArgs e)
{
myDraggables md = new myDraggables ('..some props ..');
Page.Header.Controls.Add(md.DrawJquery());
myPlaceHolder.Controls.Add(md.Draw());
}
That works, but jquery-code will become more complex. And will not be readable anymore...
how can this be achieved in json? Or another more readable way?
Thanks
If I understand you correctly, you want to write a strongly-typed .NET class in the back-end which also functions as a jQuery object on the front-end, right? If so, the best way to do it is probably to serialize your .NET object into JSON format, store it in a variable and print it out in a script tag when generating the markup. Then, on the front-end, parse the JSON string into a Javascript object and you can use it from there.
Can you post any .NET and JS code you have already?
Whenever i want to add a javascript library programatically, say jquery for example, it generally involves making sure there is a placeholder at the footer of my page, then calling a codebehind method that will take a link to the src as a parameter and return an htmlgeneric control, which is then added to this placeholder.
Is this still the neatest way to do it, even with .net 4.0 out?
I think a better way is to use the RegisterStartupScript method:
http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx
And even better in your case RegisterClientScriptInclude:
http://msdn.microsoft.com/en-us/library/kx145dw2.aspx
EDIT:
Here's a sample of RegisterClientScriptInclude:
if (!Page.ClientScript.IsClientScriptIncludeRegistered("myJsInclude"))
Page.ClientScript.RegisterClientScriptInclude("myJsInclude", "myJsFile.js");
EDIT2:
Here's a sample of an include with RegisterStartupScript:
string jsBlock = "<script src='myJsFile.js'></script>";
if (!Page.ClientScript.IsStartupScriptRegistered("myJsInclude"))
Page.ClientScript.RegisterStartupScript(typeof(string), "myJsInclude", jsBlock, false);
You should add things like language="text/javascript" to the script tag, but for readability I didn't add them.
Sorry... I decided to move my comment to an answer.
I personally add all of my JS to the ScriptManager. It helps lower the number of Http calls that the page has to make.
ScriptManager1.CompositeScript.Scripts.Add(New ScriptReference("~/Page/To/Jquery.js"))
But this is only if you're already using a ScriptManager on your page
Also, if you don't want to add it from CodeBehind, you can do it right in your page.
<ScriptManager>
<CompositeScript>
<Scripts>
<-- your scripts in here -->
</Scripts>
</CompositeScript>
</ScriptManager>
So by doing this, you're able to add all of your JS to a single HTTP Request rather than having a bunch of different requests all at once.
Then in the ScriptManager tag, you can add LoadScriptsBeforeUI="false" to have them put to the bottom of the page.
Sorry but that was never the cleanest way to inject script into an asp.net page.
Look at the ClientScript object. There are several methods that will suit your needs without resorting to placeholders.
ScriptManager is a good way to do this, as mentioned above. If you are not using MS Ajax and ScriptManager, then I suggest you write your own control. It should be very simple control at that. Add a public variable List and override RenderContents method to walk through your list of strings and render on the page. Sample code:
public class CustomScriptManager : WebControl
{
private List<string> scripts = new List<string>();
public List<string> Scripts
{
get { return scripts; }
set { scripts = value; }
}
protected override void RenderContents(HtmlTextWriter writer)
{
foreach (string script in scripts)
{
writer.Write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"" + script + "\"></script>");
}
}
}
P.S. I haven't verified above code, but I thing you get the idea.