How can I open a web api URL from within Visual Studio - asp.net-core-webapi

I have a feeling this is off topic as it's not about code but, it is something any one working in MVC and Visual Studio is likely going to find useful.
Is it possible to open a browser at the URL of the route I am working on within MVC or Web Api?
For example, if my Web Api looked like
[Route("api/[controller]")]
[ApiController]
public class MyApi : ControllerBase
{
[HttpGet]
[Route("GetItAll")]
public JsonResult Get()
{
return GetData();
}
}
I'd like to be able to, for example, right click in the Get function, and choose 'launch in browser'.
Then, Visual Studio would generate the URL (in this case .../api/MyApi/GetItAll), start debugging and navigate to this URL in the browser

Related

Using HTTPS for api routes ASP.NET

I have a asp.net application in which I used HTTP controllers (ApiController) as below
[Route("api/dashboard/{id}")]
[HttpGet]
public DataTable getDashboardDetails(int ID)
{
}
Both my web application and controllers are within same application. We have modified our website from HTTP to HTTPS.
Now, when I try to call the above api route as
https://website.com/api/dashboard/123
it throws
404 - File or directory not found
Error
Please help to get the api called. Its working in localhost.
Have you checked the ISS and visual studio configuration ?
Sometimes it need to be restarted to get this changes.

How to get domain name in ASP.NET Core 2.0 Razor Pages

I'm trying to determine the current domain name from within the Startup.Cs module of an ASP.NET Core 2.0 Razor pages application. The website will be bound to several different domains and I want to load the appropriate _layout/theme based on this.
I have been searching, but I can't figure out how to determine which domain was used to reach the site.
Any help would be greatly appreciated.
Thanks.
You have access to the request object if your controller inherits from Controller
public IActionResult About()
{
ViewData["Message"] = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";
return View();
}

VS2015 asmx method not found

I am having legacy project which was migrated to visual studio 2015. Everything works well, only asmx action is not found.
I don't understand what is wrong here? My project runs inside IIS express.
I've tried to create virtual directory inside IIS and if I open website from IIS asmx works fine.
What I've also tried is to delete and then add .asmx file to project. Nothing changed.
Only within IIS express it doesn't work (when project is run directly from visual studio). Error message:
Failed to load resource: the server responded with a status of 404
(Not Found)
http://localhost:53358/wf/wsSearchCAMERC.asmx/GetBAUSERSearch1
Settings:
Code:
namespace fCatEve
{
/// <summary>
/// Summary description for wsSearchCAMERC
/// </summary>
[WebService(Namespace = "fCatEve")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class wsSearchCAMERC : System.Web.Services.WebService
{
[WebMethod]
public string[] GetBAUSERSearch1(string prefixText, int count, string contextKey)
{
// code omitted for clarity
}
}
}
I've solved the problem by copy-pasting methods from .asmx to MVC controller, which is asynchronous by default. I can now call those methods with same link as from .asmx.

Access ASP.Net Data Cache from a Web API method in same ASP.Net project

I need to remove ASP.Net cache from inside a Web API method. This cache by the name of 'ContentNames' was set in the code-behind of an aspx page using following code. Is this possible, and if yes, then how would I access ASP.Net data cache from inside the Web API method?
The Web API and all the aspx pages are part of the same website project in Visual Studio 2013.
System.Web.HttpContext.Current.Cache.Insert("ContentNames", dt, null,
System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 60, 0));
The Web API method from where I need to access and remove 'ContentNames' data cache, looks like below.
[Authorize]
[HttpPut]
public HttpResponseMessage ApproveOrRejectContent( RadEditorContentAdminParas paras)
{
var data = GetUnApprovedContent(paras.ApprovedOrRejected, paras.PageId);
//NEED to remove a Cache by the name of 'ContentNames' ???
return Request.CreateResponse<ContentsInfoResult>(HttpStatusCode.OK, data);
}
Just add reference of System.Web to your Web Api controller and you be able to access Current cache and remove it:
System.Web.HttpContext.Current.Cache.Remove("ContentNames");

Running a part of MVC application separately under webforms application

I have a working MVC3 application with different modules. I want to use one of those modules as a separate application under IIS, this separate application will be under an already running WebForms application.
e.g.
My MVC application is running at http://mymvcappdomain.com/
There is a feature under this called "MyFeature" which runs at http://mymvcappdomain.com/MyFeature
I want another site (WebForms application) http://mywebformssitedomain/ to display exactly as http://mymvcappdomain.com/MyFeature when I browse to http://mywebformssitedomain/MyFeature
Is it possible? If so, how?
What I have already tried is dynamically registering selected number of routes based on the URL. I thought it'd work but as per the stackoverflow topic (http://stackoverflow.com/questions/2518057/request-is-not-available-in-this-context), I am not allowed to access the request object in the global.asax for registering routes.
Many thanks in advance!
Actually I was able to achieve what I wanted by changing the home controller. But it was purely because my requirements were not too complicated. The part of the MVC app I wanted to run was kind of standalone without any interference from other modules. But I guess the approach should work in resolving the issue.
public ActionResult Index()
{
if (URL is different to your normal MVC App URL)
{
return RedirectToRoute("YourCustomRouteForModule");
}
return View();
}

Resources