none
Controller method calling twice in SP Provider Hosted App with MVC 5 Razor RRS feed

  • Question

  • Hi,

    In my SharePoint Provider Hosted App in MVC 5 Razor, controller index methods are called twice every time. For example, if click on any menu item it redirect to the Index page and the page loads twice. Could anyone please help me out of this?

    Here is my menu item in layout page-

    <li class="active">@Html.ActionLink("Home", "Index", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Current.Request).AbsoluteUri }, null)</li>

    Here is my Index Method in Home Controller-

    [SharePointContextFilter]
    public ActionResult Index(string searchID, int page = 1)

    { ......................... ..................... .................... return View(productList.OrderBy(p => p.createDate).ToPagedList(page, 10)); }


    Friday, August 3, 2018 7:23 AM

Answers

  • Hi,

    Please modify the ensureSPHasRedirectedToSharePointRemoved method as below in the spcontext.js file in the add-in.

    function ensureSPHasRedirectedToSharePointRemoved() {
    	var SPHasRedirectedToSharePointParam = "&SPHasRedirectedToSharePoint=1";
    
    	var queryString = window.location.search;
    
    	if (queryString.indexOf(SPHasRedirectedToSharePointParam) >= 0) {
    	   if (window.history.pushState) {
    		  var safeUrl = window.location.pathname + queryString.replace(SPHasRedirectedToSharePointParam, '');
    		  window.history.pushState(null, document.title, safeUrl);
    	   } else {
    		  window.location.search = queryString.replace(SPHasRedirectedToSharePointParam, "");
    	   }
    	}
    }
    
    

    More information:

    https://platinumdogs.me/2016/01/11/fixing-the-page-reload-issue-for-sharepoint-asp-net-mvc-provider-hosted-addins/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, August 15, 2018 8:58 AM
    Moderator

All replies

  • Hi,

    Please check as follows:

    1. Check if view or layout page has img tag with src="#", if yes, replace it with "".

    2. Check if view or layout page has anchor tab with href="#", if yes, replace it with "".

    3. Check if it has javascript error on the page using IE F12 developer tools.

    And please provide more details(more code) about how to reproduce this issue, others would get easier to research on it.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, August 6, 2018 2:45 AM
    Moderator
  • Hi Dennis,

    Thanks for your replay.

    I have checked as you suggested,  step 1 and 2 are fine, there is no src="#" and href="#" in my layout page.

    But, found the following error on IE F12 developer tools-

    jQuery.Deferred exception: Unable to get property 'toUpperCase' of undefined or null reference TypeError: Unable to get property 'toUpperCase' of undefined or null reference
       
    at Anonymous function (https://spsite.org/SPApp/bundles/spcontext?v=t6dCJosl_mFkuP3PAvGf_hsvObYzSaxPevpD6rC7Ypg1:1:87)
       
    at Anonymous function (https://spsite.org/SPApp/bundles/jquery?v=rD9yxcIfC-_zwpaJ_9UPbUY1Niam5dFE8OFiugxkBeM1:1:820)
       
    at grep (https://spsite.org/SPApp/bundles/jquery?v=rD9yxcIfC-_zwpaJ_9UPbUY1Niam5dFE8OFiugxkBeM1:1:15468)
       
    at li (https://spsite.org/SPApp/bundles/jquery?v=rD9yxcIfC-_zwpaJ_9UPbUY1Niam5dFE8OFiugxkBeM1:1:785)
       
    at filter (https://spsite.org/SPApp/bundles/jquery?v=rD9yxcIfC-_zwpaJ_9UPbUY1Niam5dFE8OFiugxkBeM1:1:36544)
       
    at e (https://spsite.org/SPApp/bundles/spcontext?v=t6dC
    SCRIPT5007: Unable to get property 'toUpperCase' of undefined or null reference
    jquery (1,42799)

    Also found some warnings on html tags on the index pages.

    HTML1519: Invalid nesting. An "<a>" tag should not be placed within another "<a>".
    HTML1523: Overlapping end tag. Tags should be structured like "<b><i></i></b>" instead of "<b><i></b></i>".

    I also see this error and warnings are loaded twice in a single page. Please suggest what could be the next step.

    Monday, August 6, 2018 9:19 AM
  • Hi,

    Did you have some custom jQuery code? Try to debug the code using F12.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, August 6, 2018 10:02 AM
    Moderator
  • Hi Dennis,

    Everything is found fine with JQuery.

    What I got is- if the SPHostUrl contains Language, ClientTag and Product No. it doesn't load twice. 

    Presently the HostWebUrl is- 

    https://app.domain.org/Product/?SPHostUrl=https://SPSite.domain.org/Sites/Apps

    If the url is as follows, it doesn't reload- 

    https://app.domain.org/Product/?SPHostUrl=https://SPSite.domain.org/Sites/Apps&SPLanguage=en-US&SPClientTag=1&SPProductNumber=1.1234.1000

    I tried to add the additional parametesr with the SPHostUrl but it generates error.

    Wednesday, August 8, 2018 5:19 AM
  • Hi,

    Did you set query string in AppManifest.xml to {StandardTokens}?

    Please try to create a provider hosted add-in with default template of the visual studio, and check if it works. By default, the URL query string contains SPHostUrl,SPLanguage,SPClientTag,SPProductNumber,SPAppWebUrl.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, August 8, 2018 6:55 AM
    Moderator
  • Hi

    AppManifest.xml is set well-

    <StartPage>~remoteAppUrl/?{StandardTokens}</StartPage>

    It is also built on default template. I have no idea why it is happening.

    Wednesday, August 8, 2018 8:20 AM
  • Hi,

    I mean that create add-in base on default template and don't add custom code to check if it works.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Thursday, August 9, 2018 8:23 AM
    Moderator
  • Hi Dennis,

    One thing I identified, in the first call SP context is returned null, as a result the redirection status in "ShoudlRedirect" instead of "Ok". Second time the SP context is not null and the redirection status is also "Ok". Could it be the reason? Thanks.


    Tuesday, August 14, 2018 10:12 AM
  • Hi,

    Please modify the ensureSPHasRedirectedToSharePointRemoved method as below in the spcontext.js file in the add-in.

    function ensureSPHasRedirectedToSharePointRemoved() {
    	var SPHasRedirectedToSharePointParam = "&SPHasRedirectedToSharePoint=1";
    
    	var queryString = window.location.search;
    
    	if (queryString.indexOf(SPHasRedirectedToSharePointParam) >= 0) {
    	   if (window.history.pushState) {
    		  var safeUrl = window.location.pathname + queryString.replace(SPHasRedirectedToSharePointParam, '');
    		  window.history.pushState(null, document.title, safeUrl);
    	   } else {
    		  window.location.search = queryString.replace(SPHasRedirectedToSharePointParam, "");
    	   }
    	}
    }
    
    

    More information:

    https://platinumdogs.me/2016/01/11/fixing-the-page-reload-issue-for-sharepoint-asp-net-mvc-provider-hosted-addins/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, August 15, 2018 8:58 AM
    Moderator