retrieve webservice containing page control in C# - c#

is there a way to reference the page control in a webservice? something like this:
[WebService(Namespace = "http://test.org/")]
public class Search : System.Web.Services.WebService
{
public Search()
{
Page.Controls.Add(new Control()); // can I get a reference to Page?
}
}

This seems like a very odd design approach. In general a called method should have no knowledge about, or dependencies on the caller. In this case the web method would need knowledge about the page calling it. I don't think this is possible, and even if it was, consider the possibility that it may not even be a page calling the web service. It could be any kind of application.

What you are trying to do (at least the way you are trying to do it) is impossible. The web service cannot, at server side, modify the page by modifying the control tree, server side. The page object that was rendered to the user does not exists any more.
I think that what you should be doing instead is using an update panel. That will allow you to do exactly what you want to.

Related

Making a controller not visible to the client in ASP.NET WEB API (or MVC)

Let's suppose I have a layer of abstract controllers, which delegates the request to its child controller class, until it reaches the implementation.
Think of it like a pipeline of controllers, that the request must go through, and includes caching the responses, authorizing and authenticating the user, validating the input and output data, handling repository access, etc.
My leaf class (the last child of the hierarchy), may have the following signature:
public class SeasonsController : DefaultPersistenceRestController
<int, Season, SeasonPutDTO, SeasonPostDTO, SeasonQueryData> {
/** Controller implementation here **/
}
The base classes have a lot of reusable code located in one module, this is good and has helped me a lot when changing the logic of my controllers at a global level.
Now, suppose SeasonsController need to call EpisodesController, for irrelevant reasons.
The call would be like this:
EpisodesController episodeController = new EpisodesController();
//Do something with EpisodesController
The problem is that I don't want EpisodesController to be accessed from the outside, such as client's request. ASP.NET automatically identifies controllers and creates a public endpoint for them, such as http://localhost:80/episodes.
I created EpisodesController because it uses a lot of logic from the controller's base classes, but I intend to use it internally.
I can desactivate authentication, authorization, cache and all other stuff that will be useless if a controller is used in this way, so that's not a problem.
However, I cannot manage to prevent ASP.NET to ignore my EpisodesController class, and to not consider it like a controller.
Is there an attribute or annotation maybe that will tell the compiler to do this? Maybe some modification in Web.config?.
Also note that I don't want to change EpisodesController's class name to another name, as it is really a controller, but an internal one.
You could try to use the IgnoreRoute extension method. Or you could try the internal as suggested by beautifulcoder. If it's in another assembly (and you can modify it) you could also make it visible to other assemblies with InternalsVisibleToAttribute.
Although to be honest, using one controller within another controller doesn't seem right to me. I would try and refactor you common functionality to services/helpers, then you could probably also make your EpisodesController into a simple service. Composition over inheritance and all that :)
If you make a controller public it will be accessible. From what I understand, you can change it to protected or internal.

ext.net direct method, call from external class method

I have a method in a class like below:
public class ActionHelper
{
[DirectMethod]
public string DeleteComment()
{
...
return "Delete";
}
}
and I want to call it from grid command like this:
<Command Handler="Ext.net.DirectMethod.request({url: '/Classes/ActionHelper/DeleteComment', cleanRequest: true}});" />
but it's not working! how can i do that??? I use ext.net 2.2 and .netframework 4.5
look at this example
http://examples.ext.net/#/Events/DirectMethods/ID_Mode/
it can be helpful
Put a [DirectMethod] in your code behind wich calls that class and use
App.direct.<Method>(); instead
You can't, you either define the method inside the Page, User Control or any Custom Control, or you define a static method inside the Page class.
Here is a quote defining direct methods from a post in the Ext.net forums:
DirectMethod (none static, must be public): server side handler is
raised when you call special javascript method (basically, proxy
method is generated by Ext.Net toolkit). None static direct method can
be defined inside Page, User Control or any Custom Control. Please
note, if direct method is defined inside user control (master page
placeholders are user controls also) or custom control then ClientID
of that control will be added to proxy method 1
Ext.net.DirectMethods.ClientIDOfTheControl.DirectMethodName(); You can
use DirectMethodProxyID attribute for the class to define own alias or
completely remove ClientID prefix Really, none static direct method is
direct event. Single difference, that direct method has no relation
with any widget (and its events) and can be raised by developer from
javascript (as javascript method)
Static DirectMethod (must be public): similar ASP.NET PageMethods, can be defined inside Page class only. With static page method the
Page life cyle is not execued therefore access to ASP.NET control is
not possible but response time much better (depends from your method
logic only)

.NET Beans + request scope - is it possible?

I have my web application. I need, when a user do a request (I mean, call a page) create an object that store some information (it get these from database) for each request (so the data is updated at every request) and for each user (so UserA don't share the same object regard UserB).
I use to work on Java and some Web Frameworks (like Struts, Spring and JSF) and there was possible to create these objects (JavaBeans) with scope of request, and putting them visible for the whole application. I mean :
public class iContext
{
public iContext()
{
Response.Write(myBeans.Title());
}
}
it's possible without create an Istance of MyBeans every time .
I thought to put the object on session, but I prefeer to get rid about this (in fact I should check, replace, delete or insert the object every time and done tons of queries :)).
Are there some strategies to do this kind of work on .NET 3.5 and C#?
In ASP.Net it's important to know the page's life cycle. If you understand this, you could use the HttpContext.Items[] Dictionary object to fill with your custom context class in the (pre) init phase of the page. Anywhere you load data that's important to keep in memory you can add it to your context class and use it anywhere you want since HttpContext is available at all times in the scope of your page.

How to share code between Pages and Masterpages without multiple inheritance/code duplication?

I've read the questions/answers explaining that there is no multiple inheritance in C#, that we don't even need it, and that it causes too much problems.
Now, I'm working on a project where I don't really understand how can I do things without multiple inheritance, without duplicating code.
Here's the situation. There is a website with a home page and other pages inheriting from a masterpage (the home page does not inherit from). Both the page and the masterpage are performing some stuff: custom login, statistics, loading of users settings for customization, etc. For the moment, the solution is crappy, since the source code for those tasks is just copied twice.
The home page class inherits from Page. The masterpage, on the other hand, inherits from Masterpage. Logically, it would be great to inherit from a common class too, but it's multiple inheritance, so it's impossible.
So what to do instead?
I thought about several ways, but dislike them:
Create a standalone class which will be called from the page/masterpage class. So for example instead of writing bool isDisplayingTips = this.CurrentUser.IsDisplayingTips, I would write bool isDisplayingTips = this.SharedObjects.CurrentUser.IsDisplayingTips. I don't like it, since it's longer to write.
Create a "real", empty, common masterpage, and inherit both the home page and the masterpage from it. Not only it will require to write more code to access masterpage parameters, but it will also slow the things down, requiring an additional masterpage on each request.
Any idea?
MasterPage is a just control (that get embedded into the actual page) so you can not have the later approach. However, first approach of creating another helper class is quite feasible.
Yet another approach that we typically use is to have
Common base page class - all pages will inherit from the common base page.
Put common functionality in base page class
From master page, the base page can be referred by casting - for example, myBasePage = (BasePage)this.Page;. This way master page may access common functionality from base page class.
I don't find your 2nd option that dislikable.
I presume you mean creating a base class, e.g. MasterPageBase, derived from System.Web.UI.MasterPage, and creating an empty MasterPage for your homepage, that will inherit from this MasterPageBase.
If done right, it shouldn't slow things down...
I suggest you to use the first of your option. If you (understandably)
don't feel comfortable with increased level of indirection, you could just create new methods on your standalone classe, e.g:
public bool IsDisplayingTips(){
return CurrentUser.IsDisplayingTips;
}
and the from your pages just call
bool isDisplayingTips = this.SharedObjects.IsDisplayingTips()
Use:
standalone class which will be called
from the page/masterpage class
but instead of stopping there, add a base page and a base master page. Both use the shared class, and keep the specific pages/master pages code from the indirection.

Common inheritance for pages, web services and web controls

I have a set of functions I want to be available to my web pages and user controls in my c# .net 3.5 web project. My standard approach for pages is to create a "base page" that extends the System.Web.UI.Page class and then have my pages inherit from this, rather than directly from the Page class.
I now want to expose some of these functions to my web controls (ascx) and web services. I can think of a number of ways to do this, but they seem a little clumsy and I think I'm missing a trick.
Is there an easy way to provide some common functions to both my pages, web services and controls using inheritance, or do I need to wrap these functions in a class that all of them can access?
An example to clarify:
I have a singleton that handles most functionality for my web application.
At the start of each request I want to check that the class exists in the web cache and initialise it if not.
Initially this was handled in a page base that the pages all used. Now I need to be able to access my singleton safely from services and controls, with the same checks. I have therefore extracted the checking and initialisation logic into another class, that then each of my base page, control and web service, all instantiate. Even with this model I have the same code repeated in 3 places (each of my base classes for controls, ws and pages), albeit not much code, this seems wrong too!
It works, but it seems clumsy...I look forward to you guys humbling me with your wisdom!
Sounds to mee like a case of aspect-oriented programming. .NET is ill equipped for this. I'm afraid that your solution is one of the best.
Alternatively perhaps you can move all or some of those functions to a static class/singleton and then use that class from your aspx/ascx/asmx? Not much in the way of inheritance, but at least less code duplication.
My solution to this is to put all the methods and functions I want to share in my base master page class. I then put an equivalent for each method and function in the user control base class as follows:
//Property in masterpage base
public string QsSearchTerm
{
get
{
if (!String.IsNullOrEmpty(Request.QueryString["q"]))
{
return Helpers.SanitiseString(Server.UrlDecode(Request.QueryString["q"]));
}
return String.Empty;
}
}
//Property in usercontrol base
public string QsSearchTerm
{
get
{
if (Page.Master is BaseMasterPage)
{
return ((BaseMasterPage)Page.Master).QsSearchTerm;
}
return string.Empty;
}
}
What this doesn't help with, is your code repetition with web service base classes. I would think that refactoring the above into a class with a constructor that accepts an HttpContext instance would be the way forward. You can then expose a singleton instance of this class in your base web service, master page, user control, page etc.
Hope this helps, but I too would be interested in hearing if there's a better way.
In your Singleton you could provide a Strategy interface to allow variations of the code depending on the configured environment. This would allow you to switch between web/windows/wcf...and so on.
I think using a BasePage is the right approach.
I have multiple base pages and custom user controls that load differently depending on which basepage is used by the current page.
In your custom user control you can use something like:
if (this.Page is BasePageName)
{
BasePageName bp = (BasePageName)this.Page;
bp.BasePageFunction();
}
No you can get ride of the repetitive code in the custom user control and just call it from the base page.
You can also have a hierarchy of inherited base pages depending on page functionality and needs. ie.) BasePageName2 : BasePageName

Categories

Resources