What is the correct way to redirect request from www.mysite.com to www.mysite.com/swagger?
I setup an index controller Decorated with Route("") and it works but seems like a Kludge. I assume there should be a way to specify it in the MVC routing in Startup.cs, I just can't figure it out.
// kludge code, how do I do this in app.UseMvc(route => route.MapRoute...
[ApiExplorerSettings(IgnoreApi = true)]
[Route("")]
public class IndexController : Controller
{
public ActionResult Index()
{
return Redirect("/swagger");
}
}
Try the following redirect rule:
var option = new RewriteOptions();
option.AddRedirect("^$", "swagger");
app.UseRewriter(option);
Related
I want to use api version in my .net core project.Then search web and find that's solution.
Even though do exactly all solutions,but I can't get desired result.
So if any can help me,Please show me..
I add Microsoft.AspNetCore.Mvc.Versioning 4.0.0 Package in my project and ..
StartUp.cs
Then in my Controller Add Rout Attribute as Shown :
[ApiController]
[Authorize]
[Route("v{version:apiVersion}/[Controller]")]
[ApiVersion("1.0")]
public class SellerController : Controller
{
private readonly IBus _client;
private readonly string AppBaseUrl = MyHttpContext.AppBaseUrl;
//private readonly IGetUrl _globalUrl;
public SellerController(IBus client/*, IGetUrl globalUrl*/)
{
_client = client;
//_globalUrl = globalUrl;
}
[HttpGet("/Sellers/{SellerId}")] // Dashboard
public async Task<IActionResult> Info(long SellerId)
{
...
}
}
With these code I expected that I can send request to 'Info' method by this url :
But that's not working and get 404 error code status.. when I delete "/v1.0" from url and send request, that's working. I will be glad to help me .. Thanks
In your code, we can find that you applied [HttpGet("/Sellers/{SellerId}")] with route
template begin with / to Info action method, which don't get combined with route templates applied to the controller. To make request to 'Info' method, you could use below URL.
https://localhost:5090/sellers/17
I expected that I can send request to 'Info' method by this url : https://localhost:5090/v1.0/sellers/17
To achieve your requirement, you can try to modify the code like below.
[HttpGet("/v{version:apiVersion}/Sellers/{SellerId}")]
public async Task<IActionResult> Info(long SellerId)
{
//...
//for testing purpose
return Ok(SellerId);
}
Test Result
Update:
If you'd like to include v{version:apiVersion} in route template of controller level attribute routing, you can try to apply [HttpGet("{SellerId}")] to Info action method and make request with https://localhost:5090/v1.0/seller/17.
[ApiController]
[Authorize]
[Route("v{version:apiVersion}/[Controller]")]
[ApiVersion("1.0")]
public class SellerController : Controller
{
[HttpGet("{SellerId}")] // Dashboard
public async Task<IActionResult> Info(long SellerId)
{
//...
So I added a controller to a blank asp.net CORE Api.
public class IsAliveController : ControllerBase
{
[HttpPost]
[HttpGet]
[Route("isalive")]
public object Get()
{
return "I'm alive";
}
}
Then I try to access it via https://localhost:44361/isalive
and I get no response. Do I need to make some modifications to my Startup.cs?
First why have you declared your method both as a get and a post request? you can remove the post attribute.
By default your routing must start with your controller name, add this:
[Route("[controller]")]
[ApiController]
public class IsAliveController : ControllerBase
{
[HttpGet]
public object Get()
{
return "I'm alive";
}
}
Now when you call https://localhost:44361/isalive you will receive the expected response.
There are two solutions to your problem:
#1 Your controller Name is IsAlive and you have added a route attribute on your action method as isAlive, so this would work only if you call using this Url
https://localhost:44361/isalive/isalive
#2 Remove the Route attribute from the action method
public class IsAliveController : ControllerBase
{
[HttpGet]
public object Get()
{
return "I'm alive";
}
}
and you will be able to access using Url
https://localhost:44361/isalive
Found the issue. The template I choose (using Visual studio 2019) was:
Asp.net core web app > Web Application
The startup file in this template is missing a line that I needed to add:
app.UseEndpoints(endpoints =>
{
//Add this line
endpoints.MapControllers();
});
After adding that line the endpoint works without any changes to the controller.
You are able to redirect to other actions using built-in methods like:
RedirectToPage("./Index", new { StatusMessage = "Everything was processed successfully"" });
This will redirect to another PageModel that's in same directory and it's Index action. Is it possible to "extract" this functionality so that you get redirect location by just passing ./Index to some util function/method?
public class IndexModel : PageModel
{
public void OnGet()
{
string privacyPageUrl = Url.Page("./Privacy");
}
}
i have a controller in my sub domain and from this controller i want to redirect user to main domain but i use response code but it will not works how can i use redirect to action?
public ActionResult Index()
{
Response.Redirect("maindomain.com");
return View();
}
in this code when you set a break point on return View(); line response not works and breadline goes to next line.
i want user redirecting to this
url : http://www.maindomain.com
Returning the redirect ensure the code stops executing.
public ActionResult Index()
{
return Redirect("www.maindomain.com");
}
Given the following URL: http://www.domain.com/Client
is it possible to access the Route Data in a controller to determine which Controller/Action that is bound to?
It should be pretty simple to determine the controller from the RouteData dictionary, passing the key that you are looking for.
namespace UI.Controllers
{
[Authorize]
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
var controllerName = RouteData.Values["controller"];
//controllerName == "Home" at this point
var actionName = RouteData.Values["action"];
//actionName == "Index" at this point
return View("Index");
}
}
}
EDIT
I have found some information regarding how to do this here: but, you will need to change your absolute URLs back to relative URLs before you can run them through the solution provided.