I am trying to enable routing on an asp vb.net application.
I followed the next steps :
1) In global.asax,there is this code
<%@ Application Language="VB" %>
<%@ Import Namespace="System.Web.Optimization" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">
Sub Application_Start(sender As Object, e As EventArgs)
RouteConfig.RegisterRoutes(RouteTable.Routes)
BundleConfig.RegisterBundles(BundleTable.Bundles)
End Sub
</script>
2) In the RouteConfig.vb i have this :
Imports System.Web
Imports System.Web.Routing
Imports Microsoft.AspNet.FriendlyUrls
Public Module RouteConfig
Public Sub RegisterRoutes(routes As RouteCollection)
Dim settings = New FriendlyUrlSettings()
settings.AutoRedirectMode = RedirectMode.Permanent
routes.EnableFriendlyUrls(settings)
routes.MapPageRoute("Services",
"Services/{SID}",
"~/Services.aspx")
End Sub
End Module
3) In the services.aspx page, i wrote this
Imports System.Web.Routing Partial Class Services Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lblTest.Text = RouteData.Values("SID").ToString
End If
End Sub
End Class
When I run the page, I get the error message
"No object reference is specified in an object instance."
If I delete the line "lblTest.Text = RouteData.Values("SID").ToString" it is working, of course without the SID parameter
Sorry for bad english.
"No object reference is specified in an object instance" known as another wording to indicate NullReferenceException
besides common "Object reference not set to an instance of an object" message. Since you're mentioning this line:
lblTest.Text = RouteData.Values("SID").ToString
The exception occurs when RouteData.Values("SID")
contains Nothing
, and ToString
cannot work with Nothing
set as its source string. You can check if RouteData.Values
has Nothing
using RouteData.Values.ContainsKey
:
If (Page.RouteData.Values.ContainsKey("SID")) Then
lblTest.Text = RouteData.Values("SID").ToString
End If
Since you're using MapPageRoute
with FriendlyUrls
, I think you need to rename URL in second argument to another different name than physical ASPX file name, preventing Nothing
passed as URL argument:
' note 's' omitted in Service/{SID} route
routes.MapPageRoute("Services", "Service/{SID}", "~/Services.aspx")
Then, you can try reordering statements inside RegisterRoutes
so that EnableFriendlyUrls
executed after MapPageRoute
:
Public Module RouteConfig
Public Sub RegisterRoutes(routes As RouteCollection)
routes.MapPageRoute("Services", "Service/{SID}", "~/Services.aspx")
Dim settings = New FriendlyUrlSettings()
settings.AutoRedirectMode = RedirectMode.Permanent
routes.EnableFriendlyUrls(settings)
End Sub
End Module
Similar issues:
FriendlyURLs - RouteData returns no value
Page.RouteData.Values are empty for one page but not another