Liens
Templates Visual Studio
Empty |
Aucun contenu
|
Web Forms |
|
MVC |
|
Web API |
Services HTTP RESTful
|
Single Page Application |
|
Azure API App |
REST API pour Azure
|
Azure Mobile Service |
|
Astuces
|
<%-- commentaires --%>
|
Page d'index: Default.aspx
Chemins
|
var serverAbsolutePath = Request.PhysicalApplicationPath;
var serverAbsolutePath = Server.MapPath(".");
string hostedApplicationPath = HttpRuntime.AppDomainAppPath;
string absolutePath = Server.MapPath("fichier-local.txt");
string absolutePath = HttpContext.Current.Server.MapPath("fichier-local.txt");
|
URI
|
Uri uri = HttpContext.Current.Request.Url;
uri.Scheme;
uri.Host;
uri.Port;
HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
|
Transformer un string en URL
|
string urlEscapedString = Uri.EscapeUriString("http://url/brut");
string urlBrut = Uri.UnescapeUriString(urlEscapedString);
|
 |
Au contraire d'Apache, sur NGINX le single-quote n'est pas échappé en %27 |
Transformer un string pour l'affichage
|
string htmlEncodedString = Server.HtmlEncode("ma chaîne");
HttpUtility.HtmlEncode ("ma chaîne");
|
Passer des arguments à une page
 |
Request.QueryString comme Session ne peuvent être utilisés dans le constructeur de la page. |
Dans l'url
|
string valeur = Request.QueryString["argument_1"];
if (valeur != null)
{ }
|
Session
|
Session["clé"] = un_objet;
var un_objet = Session["clé"] as MaClasse;
if (un_objet != null)
{ }
|
Appeler une méthode du code-behind
|
<%@ Page Language="C#" Inherits="MonEspace.MaPage" %>
<% MaMéthode(); %>
|
|
namespace MonEspace
{
public partial class MaPage : Page
{
public void MaMéthode() {
Response.Write("Texte");
|
Changer le titre de la page
|
this.Header.Title = "Nouveau titre";
|
web.config
|
<?xml version="1.0"?>
<configuration>
<system.web>
|
web.config
|
<compilation debug="true" defaultLanguage="C#">
<assemblies></assemblies>
|
web.config
|
<customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly">
<error statusCode="500" redirect="InternalError.htm"/>
|
web.config
|
<authentication mode="None"></authentication>
|
web.config
|
<trace enabled="true" localOnly="true" pageOutput="true" />
|
web.config
|
<sessionState mode="InProc" cookieless="false" timeout="20" />
|
web.config
|
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
|
key
web.config
|
<configuration>
<appSettings>
<add key="KeyId" value="KeyValue" />
|
|
string keyValue = ConfigurationManager.AppSettings["KeyId"];
string keyValue = ConfigurationManager.AppSettings.Get("KeyId");
|
Errors
Entity xxx is not defined
web.config étant un fichier xml, il faut remplacer les & par des &
Singleton optionnel capable de gérer des évènements du niveau application.
Global.asax.cs
|
public class Global : System.Web.HttpApplication
{
protected void Application_Start (Object sender, EventArgs e) { }
protected void Session_Start (Object sender, EventArgs e) { }
protected void Application_BeginRequest (Object sender, EventArgs e) { }
protected void Application_EndRequest (Object sender, EventArgs e) { }
protected void Application_AuthenticateRequest (Object sender, EventArgs e) { }
protected void Application_Error (Object sender, EventArgs e) { }
protected void Session_End (Object sender, EventArgs e) { }
protected void Application_End (Object sender, EventArgs e) { }
}
|
La méthode est appelée à chaque fois que la page est chargée.
|
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{ }
}
|
Publier
Clique-droit sur le projet → Publish