November Happy Hour will be moved to Thursday December 5th.

UrlResolver instance is null

Vote:
 

Hi,

I am trying to get a friendly-url for a page of type ContentReference (property in site settings) in another page's controller.

In site settings:

public virtual ContentReference NewsletterSuccessPageRef { get; set; }

In the base controller I am populating the "Model" in override method OnActionExecuting():

protected override void OnActionExecuting(ActionExecutingContext filterContext)
		{
			base.OnActionExecuting(filterContext);

			var currentPage = filterContext.ActionParameters.First(ap => ap.Value is TPage).Value as TPage;
			var currentContent = filterContext.ActionParameters.First(ap => ap.Value is TContent).Value as TContent;

			Model = filterContext.ActionParameters.FirstOrDefault(ap => ap.Value is TViewModel).Value as TViewModel;

			InitializeModel(currentPage, currentContent);
		}

Other page's controller:

var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
var url = urlResolver.GetUrl(Model.SiteSettings.NewsletterSuccessPageRef);

But I get a NullReference exception on line 21:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 19: 
Line 20: 			var urlResolver = ServiceLocator.Current.GetInstance<IUrlResolver>();
Line 21: 			var url = urlResolver.GetUrl(Model.SiteSettings.NewsletterSuccessPageRef);
Line 22: 

Debugging shows that neither the "urlResolver" nor the "SiteSettings.NewsletterSuccessPageRef" are null.

Any ideas why?

Regards,

Micha

#199514
Edited, Nov 29, 2018 10:28
Vote:
 

Which version are you running? There is a bug reported for that issue, see https://world.episerver.com/support/Bug-list/bug/CMS-9578 that is fixed in CMS.Core 11.3.3

#199516
Nov 29, 2018 11:37
Vote:
 

Hi,

are yopu getting the Contentlink on your Model.Settings.NewletterSuccess?

#199525
Edited, Nov 29, 2018 15:07
Vote:
 

Running a version below 11.3.3 and currently there is no option to upgrade. I have tried the workaround suggested in the bug you linked with no success (getting the same results). Is there another way to get the friendly url of a page?

@Radek Yes, contentLink is available as usual

Thanks a lot!

#199527
Nov 29, 2018 15:22
Vote:
 

in one fragment you are asking for `UrlResolver` in another code snippet - `IUrlResolver`. are both of them `null`? If you try to get instance of resolver "somewhere" else - does it work there?

#199577
Dec 01, 2018 5:21
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.