Running Angular and Web API in Visual Studio with IIS Express - asp.net

I have a Visual Studio 2013 solution with a Web API project and a Web UI project (using Angular). I am using IIS Express.
Is there a way to set these projects up so that the Angular code can call the Web API project without hard-coding in the localhost and port number?
return $http.get("http://localhost:1561/api/products")
.then(function (response) {
return response.data;
});
If I hard-code localhost:1561 instead of just using the "/api/products" style I have to manually change the code before deploying to production and change it back to run it during development.
Is there an easier way?
Thanks!

var rootPath;
if (location.hostname === 'localhost') {
rootPath = 'http://localhost:1561';
} else {
rootPath = 'http://' + location.hostname;
}
return $http.get(rootPath + '/api/products')
.then(function (response) {
return response.data;
});

I actually wrote a simple utility method that returns the absolute URL so in your code you just have to type the relative URL. Below is this method which you should be able to call from your JS passing the relative URL (i.e. /api/products) or by converting it into a helper extension method...
// Code
public static string ToAbsoluteUrl(string relativeUrl)
{
if (string.IsNullOrEmpty(relativeUrl))
return relativeUrl;
if (HttpContext.Current == null)
return relativeUrl;
if (relativeUrl.StartsWith("/"))
relativeUrl = relativeUrl.Insert(0, "~");
if (!relativeUrl.StartsWith("~/"))
relativeUrl = relativeUrl.Insert(0, "~/");
var url = HttpContext.Current.Request.Url;
var port = url.Port != 80 ? (":" + url.Port) : String.Empty;
return String.Format("{0}://{1}{2}{3}",
url.Scheme, url.Host, port, VirtualPathUtility.ToAbsolute(relativeUrl));
}

From what I understand, it is not possible to do what I was attempting to do. Here are the options:
1) Use IIS on the local machine. That way you can set up the paths/virtual directories as necessary. This also makes it easier to call the API from other browsers during debugging. Note: This does require that you then run Visual Studio in admin mode from this point forward.
2) Put the two projects into one. For me this was not a valid option because I am need to deliver the UI code completely separate from the API code.
Hope this helps others trying to work with Angular and Web API.

Related

Is Azure Function to Function authentication with MSI supported

I created 2 Azure Function Apps, both setup with Authentication/Authorization so an AD App was created for both. I would like to setup AD Auth from one Function to the other using MSI. I setup the client Function with Managed Service Identity using an ARM template. I created a simple test function to get the access token and it returns: Microsoft.Azure.Services.AppAuthentication: Token response is not in the expected format.
try {
var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://myapp-registration-westus-dev.azurewebsites.net/");
log.Info($"Access Token: {accessToken}");
return req.CreateResponse(new {token = accessToken});
}
catch(Exception ex) {
log.Error("Error", ex);
throw;
}
Yes, there is a way to do this. I'll explain at a high level, and then add an item to the MSI documentation backlog to write a proper tutorial for this.
What you want to do is follow this Azure AD authentication sample, but only configure and implement the parts for the TodoListService: https://github.com/Azure-Samples/active-directory-dotnet-daemon.
The role of the TodoListDaemon will be played by a Managed Service Identity instead. So you don't need to register the TodoListDaemon app in Azure AD as instructed in the readme. Just enable MSI on your VM/App Service/Function.
In your code client side code, when you make the call to MSI (on a VM or in a Function or App Service), supply the TodoListService's AppID URI as the resource parameter. MSI will fetch a token for that audience for you.
The code in the TodoListService example will show you how to validate that token when you receive it.
So essentially, what you want to do is register an App in Azure AD, give it an AppID URI, and use that AppID URI as the resource parameter when you make the call to MSI. Then validate the token you receive at your service/receiving side.
Please check that the resource id used "https://myapp-registration-westus-dev.azurewebsites.net/" is accurate. I followed steps here to setup Azure AD authentication, and used the same code as you, and was able to get a token.
https://learn.microsoft.com/en-us/azure/app-service/app-service-mobile-how-to-configure-active-directory-authentication
You can also run this code to check the exact error returned by MSI. Do post the error if it does not help resolve the issue.
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Secret", Environment.GetEnvironmentVariable("MSI_SECRET"));
var response = await client.GetAsync(String.Format("{0}/?resource={1}&api-version={2}", Environment.GetEnvironmentVariable("MSI_ENDPOINT"), "https://myapp-registration-westus-dev.azurewebsites.net/", "2017-09-01"));
string msiResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
log.Info($"MSI Response: {msiResponse}");
Update:-
This project.json file and run.csx file work for me. Note: The project.json refers to .NET 4.6, and as per Azure Functions documentation (link in comments), .NET 4.6 is the only supported version as of now. You do not need to upload the referenced assembly again. Most probably, incorrect manual upload of netstandard assembly, instead of net452 is causing your issue.
Only the .NET Framework 4.6 is supported, so make sure that your
project.json file specifies net46 as shown here. When you upload a
project.json file, the runtime gets the packages and automatically
adds references to the package assemblies. You don't need to add #r
"AssemblyName" directives. To use the types defined in the NuGet
packages, add the required using statements to your run.csx file.
project.json
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.Services.AppAuthentication": "1.0.0-preview"
}
}
}
}
run.csx
using Microsoft.Azure.Services.AppAuthentication;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
try
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://vault.azure.net/");
log.Info($"Access Token: {accessToken}");
return req.CreateResponse(new {token = accessToken});
}
catch(Exception ex)
{
log.Error("Error", ex);
throw;
}
}

Lightswitch HTML - chat application upgrade

I wanted to know if anyone has given this a go and got it working?
http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/182/Connecting-To-SignalR-With-LightSwitch-HTML-Client.aspx
Basically my issue is i cant open the file to begin with, whether it be Visual Studio 2012, 2013 or 2015, so I have followed the guide and used the files from the downloaded project for this error message to occur:
which is caused by this line under the sendmessage_execute function:
chat.server.send(screen.displayname, screen.message);
im hoping someone has got this working and could point out anything different from the user guide, heres what I have used/done:
Under the PROJECT.Server I have:
created a folder called SignalR with the file ChatHub.cs in
added the json2.js (both) and signalR (both) files to the scripts
folder
Under the Project.HTMLClient
added the json2.js (both) and signalR (both) files to the scripts
folder
referenced the scripts including the localhost in the default.htm file
Created a screen. called ChatScreen and added all the referenced code here from the guide online (3 strings and 1 button)
i also installed the Nu-GET as instructed
More research for this was required, I found this post which explains how to do it a lot easier and in an application for both 2013/2015, works a treat and can easily be adapted for other screens
https://blogs.msdn.microsoft.com/rmattsampson/2013/03/14/asp-net-signalr-and-lightswitch-vs-2012-update-2-vs-2013-and-later/
I have also managed to edit there block of code to list all messages in a string, this is not stored and refresh's each time but its now possible to have a conversation while on the same screen
var string;
myapp.ChatScreen.created = function (screen) {
string = "";
$(function () {
chat = $.connection.chatHub;
chat.client.broadcastMessage = function (message) {
string = string + message + ";";
screen.updates = string.split(';').join("\r\n");
console.log(string.split(';').join("\r\n"))
};
$.connection.hub.start()
.done(function () {
})
.fail(function () {
alert("Could not Connect! - ensure EnableCrossDomain = true");
});
});
};
It would be better practice using an array and displaying it this way but the code above demonstrates it works

HTTP requests in Intel XDK

I previously built an app in the Intel XDK platform pre the Feb 23rd update and now the software has updated when i try to run the emulator it just crashes.
previously i sent a get request to a process php page for a login in the following way.
$(document).ready(function(){
$('form.login').submit(function () {
var user = $(this).find("[name='user']").val();
var pass = $(this).find("[name='pass']").val();
var sublogin = $(this).find("[name='sublogin']").val();
// ...
$.ajax({
type: "POST",
url: "http://www.domain.com/data/apps/project1/process.php",
data: {
user : user,
pass : pass,
sublogin : sublogin,
},
success: function(response){
if(response == "1")
{
$("#responsecontainer").html(response);
window.location.href = "menu.html";
}
// Login failed
else
{
$("#responsecontainer").html(response);
}
//alert(response);
}
});
this.reset();
return false;
});
});
However it seems that this is the piece of code that is causing the problems, if I remove this item of code the project no longer crashes.
When i read through the Intel XDK documents it only shows HTTP request to call XML files.
So i was hoping that somebody may know why this is causing the problem or how i might construct it so that Intel XDK doesn't crash.
There is a regression bug with regards to relative location URL referenced through emulator, a fix is being worked on. This is related to emulator only. Your app should work fine with test tab using App Preview on the device and using the build.
Till we come up with a fix for emulator crash, here is a workaround. The issue arises when you are trying to change the location of your current page with window.location.href = "menu.html"; and emulator is not able to resolve the relative path during ajax call.
Please use the following code as a workaround.
var newLocation = 'menu.html';
if ( window.tinyHippos ) {
// special case for emulator
newLocation = getWebRoot() + newLocation;
}
document.location.href=newLocation;
function getWebRoot() {
"use strict" ;
var path = window.location.href ;
path = path.substring( 0, path.lastIndexOf('/') ) ;
path += '/';
return path;
}
Swati

How to get the thin client URI of an Alfresco folder/document?

A thin client URI is a web address that you can type to see details about a file or folder, on a nice web user interface.
For instance, my Android app uses Alfresco's CMIS API, but for complex operations (eg. to start a workflow on this file), you could click on a link and it would bring you to the fully-fledged web interface (provided by the Alfresco server).
How to calculate this thin client URI, for any Alfresco folder/document?
A good start is to use the thinClientURI feature of the CMIS protocol. Unfortunately it only work for the root of the repository.
A perfect algorithm would show Alfresco Share nodes in their Alfresco Share site, rather than in Share's generic Repository Browser.
Alfresco does have a little known feature to do just what you need! I believe it was implemented in Enterprise 4.0.3ish, ready for 4.1, and the main use of it so far is in Cloud Sync.
The webscript you're looking for is org.alfresco.repository.site.site-share-view-url.get and it is exposed as /api/sites/shareUrl?nodeRef=nodeRef . It returns a simple bit of JSON, such as:
{
"site": "alfresco-test",
"url": "https:\/\/my.alfresco.com\/share\/test.com\/page\/site\/alfresco-test\/document-details?nodeRef=workspace:\/\/SpacesStore\/aae3b33fd-23d4-4091-ae64-44a8e332091341"
}
(The above example is taken from the Alfresco cloud version, but it should be present in late 4.0 enterprise releases, enterprise 4.1, and community + enterprise 4.2 onwards)
If you want to see what kinds of content it supports, your best bet is to look at the java class which powers it, org.alfresco.repo.web.scripts.site.SiteShareViewUrlGet
However, one slight restriction is that it only supports nodes that are located within sites. If you have a non-site node, you'll have to calculate a repository browser URL for it yourself...
Below is my current implementation.
It is (very) far from perfect, as it only works for folders, and only in Alfresco Share.
string suffix1 = "alfresco/cmisatom";
string suffix2 = "alfresco/service/cmis";
if (repo.Address.AbsoluteUri.EndsWith(suffix1) || repo.Address.AbsoluteUri.EndsWith(suffix2))
{
// Detect suffix length.
int suffixLength = 0;
if (repo.Address.AbsoluteUri.EndsWith(suffix1))
suffixLength = suffix1.Length;
if (repo.Address.AbsoluteUri.EndsWith(suffix2))
suffixLength = suffix2.Length;
string root = repo.Address.AbsoluteUri.Substring(0, repo.Address.AbsoluteUri.Length - suffixLength);
if (repo.RemotePath.StartsWith("/Sites"))
{
// Case of Alfresco Share.
// Example RemotePath: /Sites/thesite
// Result: http://server/share/page/site/thesite/documentlibrary
// Example RemotePath: /Sites/thesite/documentLibrary/somefolder/anotherfolder
// Result: http://server/share/page/site/thesite/documentlibrary#filter=path|%2Fsomefolder%2Fanotherfolder
// Example RemotePath: /Sites/s1/documentLibrary/éß和ệ
// Result: http://server/share/page/site/s1/documentlibrary#filter=path|%2F%25E9%25DF%25u548C%25u1EC7
// Example RemotePath: /Sites/s1/documentLibrary/a#bc/éß和ệ
// Result: http://server/share/page/site/thesite/documentlibrary#filter=path%7C%2Fa%2523bc%2F%25E9%25DF%25u548C%25u1EC7%7C
string path = repo.RemotePath.Substring("/Sites/".Length);
if (path.Contains("documentLibrary"))
{
int firstSlashPosition = path.IndexOf('/');
string siteName = path.Substring(0, firstSlashPosition);
string pathWithinSite = path.Substring(firstSlashPosition + "/documentLibrary".Length);
string escapedPathWithinSite = HttpUtility.UrlEncode(pathWithinSite);
string reescapedPathWithinSite = HttpUtility.UrlEncode(escapedPathWithinSite);
string sharePath = reescapedPathWithinSite.Replace("%252f", "%2F");
return root + "share/page/site/" + siteName + "/documentlibrary#filter=path|" + sharePath;
}
else
{
// Site name only.
return root + "share/page/site/" + path + "/documentlibrary";
}
}
else
{
// Case of Alfresco Web Client. Difficult to build a direct URL, so return root.
return root;
}
}

Changing URLs while moving project from development server to live server

This is relatively a noob question.
While developing a ASP.Net website, if I am referring to a resource with ResolveUrl() method, it works on either live server or dev server but not on both, based on the Url provided.
For example, if my code tries to pick a resource with below code, it works on live server but not on development server as JScript.js is not under http://localhost:xx/Assets but is under http://localhost:xx/ApplicationName/Assets.
<script src='<%# ResolveUrl("~/Assets/JScript.js")%>' type="text/javascript"></script>
In order to make it work on both servers, I have to keep changing the URL according to the server I am working on.
I have been suffering this annoying problem for sometime but kept ignoring it.
Is there a better way to do it?
Thanks!
As posted in my comments above, you want to use a <%= %> vs. <%# %> to render an inline expression. You only use the hash symbol for inline data binding.
I know this might not be the out-the-box way, but I use these to make sure URLS are corect within my applications without issues. with these available in my Page/View's base classes...
public static string ApplicationRootUrl()
{
string port = String.Empty;
if (HttpContext.Current.Request.ServerVariables["SERVER_PORT"] != null && HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString() != "80" && HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString() != "443")
{
port = String.Concat(":", HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString());
}
string protocol = "http://";
if (HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"] != null && HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"] != "0")
{
protocol = "https://";
}
return String.Concat(protocol, String.Concat(HttpContext.Current.Request.Url.Host, port, HttpContext.Current.Request.ApplicationPath, '/').Replace(#"//", #"/").ToLower());
}
/// <summary>
/// Expands a virtual URL to an absolute URL using the current application root url
/// </summary>
/// <param name="url"></param>
public static string ExpandUrl(string url)
{
if (url.Trim().StartsWith("~"))
{
return String.Concat(ApplicationRootUrl(), url.Substring(1).Replace("//", "/"));
}
if (url.Trim().StartsWith("www", StringComparison.OrdinalIgnoreCase))
{
return String.Concat("http://", url);
}
return url;
}
Try making http://localhost:xx/ApplicationName a virtual app on the DEV machine. That way ~/Assets will be in the root of the app on both PROD and DEV
Basically, the "Assets" directory needs to be in the root of your App, so you need to make the parent of "Assets" the App root on both Dev and PROD.

Resources