با سلام
با استفاده از یه نمونه کد url routing رو داخل پروژه ام اجرا کردم
با وجود اینکه همه موارد و مشابه نمونه کد انجام دادم ولی RouteData.Values.ContainsValue("formID") خالی برگردانده میشه
ممکنه چیزی در پروژه من باعث null شدن ولیو بشه؟
Form1 :
public void Fill_News() { var QueryFill_news = (from pro in DataClass.Tbl_News where pro.Id_Parent == 2 && pro.Id_Child == 27 orderby pro.Date_News descending select pro); foreach (var item in QueryFill_news) { litral_News.Text += string.Format(@" <div class='item'> <div class='item-header item-header-hover'> <div class='item-header-hover-buttons'> <span data-hover-text-me='مطالعه خبر'><a href='post.aspx?id={0}' class='fa fa-mail-reply'></a></span> <span data-hover-text-me='مشاهده نظرات'><a href='post.aspx?id={1}#comments' class='fa fa-comment'></a></span> </div> <a href='post.aspx?id={2}'><img src='ketab/Image_ketab/Image_News/{3}' alt='{4}' /></a> </div> <div class='item-content'> <h3> <a href='WebForm2/{5}/{6}'> {7} </a> </h3> <p> {8} </p> </div> </div>", item.Id_News, item.Id_News, item.Id_News, item.First_ImageForCover, item.Title_News, item.Id_News, item.Title_News.ToString().Replace(' ', '-'), item.Title_News, item.Sub_Title); }
Form2 :
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (!RouteData.Values.ContainsValue("formID")) { } else { int Id_news = Convert.ToInt32(Page.RouteData.Values["formID"]); lbl_result_id.Text = Page.RouteData.Values["formID"].ToString(); lbl_result_title.Text = Page.RouteData.Values["formTitle"].ToString(); } } }
Global :
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { RouteConfig.RegisterRoutes(RouteTable.Routes); RoutingSite(RouteTable.Routes); } public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.EnableFriendlyUrls(); } } public static void RoutingSite(RouteCollection route) { route.MapPageRoute("post", "post/{PID}/{*PTitle}", "~/post.aspx"); route.MapPageRoute("WebForm2", "WebForm2/{formID}/{*formTitle}", "~/WebForm2.aspx"); } }
خود url را قرار دهید
ممکنه آددرس رو درست ارسال نمیکنید
<form id="form1" runat="server"> <div> <a href="WebForm2/12/آموزش-برنامه-نویسی">آموزش برنامه نویسی</a> </div> </form>
<form id="form2" runat="server"> <div> <p> Product ID : <asp:Label runat="server" ID="lblPID"></asp:Label></p> <p> Product Title : <asp:Label runat="server" ID="lblPT"></asp:Label></p> <br /> </div> </form>
یو آر ال رو درست نشون میده ولی در بیهایند کد به ای دی و تایتل دسترسی ندارم
متاسفانه کد من هم مشابه نمونه ی ارسالی شماست ولی Page.RouteData.Values برابر با نال میشه
ممکنه چیزی در وب کانفیگ باعث null شدنش بشه؟!!
<?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <add name="Db_NewsConnectionString" connectionString="Data Source=.;Initial Catalog=Db_News;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <system.webServer> <rewrite> <rules> <!--Never display default.aspx--> <rule name="index.aspx Redirect" stopProcessing="true"> <match url="^(.*\/)*index\.aspx$" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" /> </conditions> <action type="Redirect" url="{R:1}" redirectType="Permanent"/> </rule> <!--Redirect and force URls to lowercase using IIS, Web.config and the URL Rewrite function--> <rule name="LowerCaseRule1" stopProcessing="true"> <match url="[A-Z]" ignoreCase="false" /> <action type="Redirect" url="{ToLower:{URL}}" /> </rule> <!--Remove .aspx from pages using URL Rewrite module in IIS for asp.net--> <rule name="Redirect to clean URL" stopProcessing="true"> <match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/> <action type="Redirect" url="{R:1}"/> </rule> </rules> </rewrite> <modules runAllManagedModulesForAllRequests="true"> <remove name="BundleModule" /> <add name="BundleModule" type="System.Web.Optimization.BundleModule" /> </modules> <defaultDocument> <files> <add value="index.aspx"/> </files> </defaultDocument> </system.webServer> <system.web> <urlMappings enabled="true"> <add url="~/index" mappedUrl="~/index.aspx" /> <add url="~/about" mappedUrl="~/about.aspx" /> <add url="~/search" mappedUrl="~/search.aspx" /> </urlMappings> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> <pages validateRequest="false" /> <customErrors mode="on"> <error statusCode="404" redirect="404-page.html"/> </customErrors> </system.web> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/> </compilers> </system.codedom> <appSettings> <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/> </appSettings> </configuration>
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)